11{{#template name="tutorialAngular2.step_09.html"}}
2+ {{> downloadPreviousStep stepName="step_08"}}
23
34Right now our app has no privacy, every user can see all the parties on the screen.
45
@@ -17,6 +18,8 @@ Write this command in the console:
1718
1819Now run the app. You can't see any parties.
1920
21+ {{> DiffBox tutorialName="angular2-meteor" step="9.1"}}
22+
2023## Meteor.publish
2124
2225So 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.
2932
3033Inside the file insert this code:
3134
32- __ ` server/parties.ts ` :__
33-
34- Meteor.publish("parties", function () {
35- return Parties.find();
36- });
35+ {{> DiffBox tutorialName="angular2-meteor" step="9.2"}}
3736
3837Let's see what's happening here:
3938
@@ -53,35 +52,12 @@ Using [Meteor.subscribe](http://docs.meteor.com/#/full/meteor_subscribe) we can
5352
5453You can add this to our PartiesList component.
5554
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"}}
6656
6757
6858Now let's limit the data sent to client.
6959
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"}}
8561
8662Our publish function can also take parameters. In that case, we would also need to pass the parameters from the client.
8763
@@ -93,37 +69,19 @@ Reset the database from the console:
9369
9470We'll add a different set of data in ` loadParties.ts ` :
9571
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"}}
11473
11574Run the app again, and you should see only two items. The third being ` public: false ` and hidden.
11675
76+ Let's also update our IParty interface to include the new key: ` isPublic ` .
77+
78+ {{> DiffBox tutorialName="angular2-meteor" step="9.6"}}
79+
11780## Meteor subscribe with params
11881
11982We can use these parameters to limit the items we are subscribing to.
12083
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"}}
12785
12886In the second parameter, our function uses the Mongo API to return the wanted documents (document are the JSON-style data structure of MongoDB).
12987
@@ -139,29 +97,17 @@ So now let's add the `isPublic` flag to the parties and see how it affects the p
13997
14098Let's add a checkbox to the new party form in ` parties-form.ng.html ` :
14199
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"}}
151101
152- <label>Public</label>
153- <input type="checkbox" ng-control="isPublic">
102+ {{> DiffBox tutorialName="angular2-meteor" step="9.9"}}
154103
155104> Checkbox currently not working. You can use ` [checked]="isPublic" (click)="toggleCheck()" ` for now if you need a work around.
156105
157106Notice how easy it is to bind a checkbox to a model with Angular 2!
158107
159108Let's add the same to the ` party-details.ng.html ` page:
160109
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"}}
165111
166112Now let's run the app.
167113
@@ -182,11 +128,7 @@ So let's start with defining our publish function.
182128
183129Create a new file under the ` server ` folder named ` users.ts ` and place the following code in:
184130
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"}}
190132
191133So 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.
192134
@@ -197,14 +139,7 @@ The emails field holds all the user's email addresses, and the profile might hol
197139
198140Now let's subscribe to that publish method in our PartyDetails component.
199141
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"}}
208143
209144* We bind to the Meteor.users collection
210145* 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.
213148
214149Add this ng-for list to the end of ` parties-details.ng.html ` . Don't forget to import ` NgFor ` and add it as a directive.
215150
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"}}
233152
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"}}
240154
241155Run 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.
242156
0 commit comments