1
1
{{#template name="tutorialAngular2.step_09.html"}}
2
+ {{> downloadPreviousStep stepName="step_08"}}
2
3
3
4
Right now our app has no privacy, every user can see all the parties on the screen.
4
5
@@ -17,6 +18,8 @@ Write this command in the console:
17
18
18
19
Now run the app. You can't see any parties.
19
20
21
+ {{> DiffBox tutorialName="angular2-meteor" step="9.1"}}
22
+
20
23
## Meteor.publish
21
24
22
25
So now we need to tell Meteor what parties should it publish to the clients.
@@ -29,11 +32,7 @@ Let's create a new file named `parties.ts` inside the server folder.
29
32
30
33
Inside the file insert this code:
31
34
32
- __ ` server/parties.ts ` :__
33
-
34
- Meteor.publish("parties", function () {
35
- return Parties.find();
36
- });
35
+ {{> DiffBox tutorialName="angular2-meteor" step="9.2"}}
37
36
38
37
Let's see what's happening here:
39
38
@@ -53,35 +52,12 @@ Using [Meteor.subscribe](http://docs.meteor.com/#/full/meteor_subscribe) we can
53
52
54
53
You can add this to our PartiesList component.
55
54
56
- __ ` client/parties-list/parties-list.ts ` :__
57
-
58
- export class PartiesList {
59
- constructor () {
60
- Meteor.subscribe('parties');
61
- Tracker.autorun(zone.bind(() => {
62
- this.parties = Parties.find().fetch();
63
- }));
64
- }
65
- }
55
+ {{> DiffBox tutorialName="angular2-meteor" step="9.3"}}
66
56
67
57
68
58
Now let's limit the data sent to client.
69
59
70
- __ ` server/parties.ts ` :__
71
-
72
- Meteor.publish("parties", function () {
73
- return Parties.find({
74
- $or:[
75
- {$and:[
76
- {"isPublic": true},
77
- {"isPublic": {$exists: true}}
78
- ]},
79
- {$and:[
80
- {owner: this.userId},
81
- {owner: {$exists: true}}
82
- ]}
83
- ]});
84
- });
60
+ {{> DiffBox tutorialName="angular2-meteor" step="9.4"}}
85
61
86
62
Our publish function can also take parameters. In that case, we would also need to pass the parameters from the client.
87
63
@@ -93,37 +69,19 @@ Reset the database from the console:
93
69
94
70
We'll add a different set of data in ` loadParties.ts ` :
95
71
96
- __ ` server/parties.ts ` :__
97
-
98
- var parties:IParty[] = [{
99
- name: 'Dubstep-Free Zone',
100
- description: 'Can we please just for an evening not listen to dubstep.',
101
- owner: 'anonymous',
102
- isPublic: true
103
- }, {
104
- name: 'All dubstep all the time',
105
- description: 'Get it on!',
106
- owner: 'anonymous',
107
- isPublic: true
108
- }, {
109
- name: 'Savage lounging',
110
- description: 'Leisure suit required. And only fiercest manners.',
111
- owner: 'anonymous',
112
- isPublic: false
113
- }];
72
+ {{> DiffBox tutorialName="angular2-meteor" step="9.5"}}
114
73
115
74
Run the app again, and you should see only two items. The third being ` public: false ` and hidden.
116
75
76
+ Let's also update our IParty interface to include the new key: ` isPublic ` .
77
+
78
+ {{> DiffBox tutorialName="angular2-meteor" step="9.6"}}
79
+
117
80
## Meteor subscribe with params
118
81
119
82
We can use these parameters to limit the items we are subscribing to.
120
83
121
- __ ` client/party-details/party-details.ts ` :__
122
-
123
- onActivate() {
124
- Meteor.subscribe('parties', this.partyId);
125
- this.party = Parties.find(this.partyId).fetch();
126
- }
84
+ {{> DiffBox tutorialName="angular2-meteor" step="9.7"}}
127
85
128
86
In the second parameter, our function uses the Mongo API to return the wanted documents (document are the JSON-style data structure of MongoDB).
129
87
@@ -139,29 +97,17 @@ So now let's add the `isPublic` flag to the parties and see how it affects the p
139
97
140
98
Let's add a checkbox to the new party form in ` parties-form.ng.html ` :
141
99
142
- __ ` client/parties-form/parties-form.ts ` :__
143
-
144
- this.partiesForm = new ControlGroup({
145
- name: new Control('', Validators.required),
146
- description: new Control('', Validators.required),
147
- isPublic: new Control(false)
148
- });
149
-
150
- __ ` client/parties-form/parties-form.ng.html ` :__
100
+ {{> DiffBox tutorialName="angular2-meteor" step="9.8"}}
151
101
152
- <label>Public</label>
153
- <input type="checkbox" ng-control="isPublic">
102
+ {{> DiffBox tutorialName="angular2-meteor" step="9.9"}}
154
103
155
104
> Checkbox currently not working. You can use ` [checked]="isPublic" (click)="toggleCheck()" ` for now if you need a work around.
156
105
157
106
Notice how easy it is to bind a checkbox to a model with Angular 2!
158
107
159
108
Let's add the same to the ` party-details.ng.html ` page:
160
109
161
- __ ` client/party-details/party-details.ng.html ` :__
162
-
163
- <label>Is public</label>
164
- <input type="checkbox" [(ng-model)]="party.public">
110
+ {{> DiffBox tutorialName="angular2-meteor" step="9.9"}}
165
111
166
112
Now let's run the app.
167
113
@@ -182,11 +128,7 @@ So let's start with defining our publish function.
182
128
183
129
Create a new file under the ` server ` folder named ` users.ts ` and place the following code in:
184
130
185
- __ ` server/users.ts ` :__
186
-
187
- Meteor.publish("users", function () {
188
- return Meteor.users.find({}, {fields: {emails: 1, profile: 1}});
189
- });
131
+ {{> DiffBox tutorialName="angular2-meteor" step="9.11"}}
190
132
191
133
So here again we use the Mongo API to return all the users (find with an empty object) but we select to return only the emails and profile fields.
192
134
@@ -197,14 +139,7 @@ The emails field holds all the user's email addresses, and the profile might hol
197
139
198
140
Now let's subscribe to that publish method in our PartyDetails component.
199
141
200
- __ ` client/party-details/party-details.ts ` :__
201
-
202
- onActivate() {
203
- Meteor.subscribe('users');
204
- this.users = Meteor.users();
205
-
206
- ...
207
- }
142
+ {{> DiffBox tutorialName="angular2-meteor" step="9.12"}}
208
143
209
144
* We bind to the Meteor.users collection
210
145
* Binding the result to this.users
@@ -213,30 +148,9 @@ Now let's add the list of users to the view to make sure it works.
213
148
214
149
Add this ng-for list to the end of ` parties-details.ng.html ` . Don't forget to import ` NgFor ` and add it as a directive.
215
150
216
- __ ` client/party-details/party-details.ts ` :__
217
-
218
- import {NgFor} from 'angular2/angular2';
219
-
220
- @View({
221
- templateUrl: 'client/party-details/party-details.ng.html',
222
- directives: [FORM_DIRECTIVES, RouterLink, NgFor]
223
- })
224
- export class PartyDetails {
225
- ...
226
- onActivate() {
227
- this.users = Meteor.users;
228
- ...
229
- }
230
- }
231
-
232
- __ ` client/party-details/party-details.ng.html ` :__
151
+ {{> DiffBox tutorialName="angular2-meteor" step="9.13"}}
233
152
234
- <ul>
235
- Users:
236
- <li *ng-for="#user of users">
237
- <div>{{dstache}} user.emails[0].address }}</div>
238
- </li>
239
- </ul>
153
+ {{> DiffBox tutorialName="angular2-meteor" step="9.14"}}
240
154
241
155
Run the app and see the list of all the users' emails that created a login and password and did not use a service to login.
242
156
0 commit comments