Skip to content

Commit

Permalink
Adding ability to subscribe also in bindOne method
Browse files Browse the repository at this point in the history
  • Loading branch information
Urigo committed Oct 13, 2014
1 parent dd92aed commit 6d57047
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ For example:
| model | String | The scope property the model will be bound to. | Yes | |
| id | String | The id used to look up the model from the collection | Yes | |
| 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 |
| 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 |

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

Expand Down
21 changes: 20 additions & 1 deletion modules/angular-meteor-collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ angularMeteorCollections.factory('$collection', ['$q', 'HashKeyCopier', '$subscr
}
return {

bindOne: function(scope, model, id, auto) {
bindOne: function(scope, model, id, auto, publisher) {
Deps.autorun(function(self) {
scope[model] = collection.findOne(id);
if (!scope.$$phase) scope.$apply(); // Update bindings in scope.
Expand All @@ -26,6 +26,25 @@ angularMeteorCollections.factory('$collection', ['$q', 'HashKeyCopier', '$subscr
collection.update({_id: newItem._id}, { $set: _.omit(newItem, '_id') });
}, true);
}

var deferred = $q.defer();

if (publisher) { // Subscribe to a publish method
var publishName = null;
if (publisher === true)
publishName = collection._name;
else
publishName = publisher;

$subscribe.subscribe(publishName).then(function(){
deferred.resolve(scope[model]);
});

} else { // If no subscription, resolve immediately
deferred.resolve(scope[model]);
}

return deferred.promise;
},

bind: function (scope, model, auto, publisher) {
Expand Down

0 comments on commit 6d57047

Please sign in to comment.