Skip to content

Commit 6d57047

Browse files
committed
Adding ability to subscribe also in bindOne method
1 parent dd92aed commit 6d57047

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ For example:
157157
| model | String | The scope property the model will be bound to. | Yes | |
158158
| id | String | The id used to look up the model from the collection | Yes | |
159159
| auto | Boolean | By default, changes in the model will not automatically update the collection. However if set to true, changes in the client will be automatically propagated back to the collection. A deep watch is created when this is set to true, which sill degrade performance. | No | false |
160+
| publisher | Boolean/String | By default, bindOne method will not automatically subscribe to the collection. However if set to true, bind will call Meteor.subscribe on the current collection. you can also set publisher to a string and then bind will call Meteor publish with that string. | No | false |
160161

161162
[More in step 6 of the tutorial](http://angularjs.meteor.com/tutorial/step_06)
162163

modules/angular-meteor-collections.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ angularMeteorCollections.factory('$collection', ['$q', 'HashKeyCopier', '$subscr
1010
}
1111
return {
1212

13-
bindOne: function(scope, model, id, auto) {
13+
bindOne: function(scope, model, id, auto, publisher) {
1414
Deps.autorun(function(self) {
1515
scope[model] = collection.findOne(id);
1616
if (!scope.$$phase) scope.$apply(); // Update bindings in scope.
@@ -26,6 +26,25 @@ angularMeteorCollections.factory('$collection', ['$q', 'HashKeyCopier', '$subscr
2626
collection.update({_id: newItem._id}, { $set: _.omit(newItem, '_id') });
2727
}, true);
2828
}
29+
30+
var deferred = $q.defer();
31+
32+
if (publisher) { // Subscribe to a publish method
33+
var publishName = null;
34+
if (publisher === true)
35+
publishName = collection._name;
36+
else
37+
publishName = publisher;
38+
39+
$subscribe.subscribe(publishName).then(function(){
40+
deferred.resolve(scope[model]);
41+
});
42+
43+
} else { // If no subscription, resolve immediately
44+
deferred.resolve(scope[model]);
45+
}
46+
47+
return deferred.promise;
2948
},
3049

3150
bind: function (scope, model, auto, publisher) {

0 commit comments

Comments
 (0)