Skip to content

Commit

Permalink
Adding $subscribe to support subscription with promises and added aut…
Browse files Browse the repository at this point in the history
…o subscribe option to $collection.bind
  • Loading branch information
Urigo committed Oct 1, 2014
1 parent 3fe5bc7 commit 3436e75
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 11 deletions.
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ ngMeteor provides an AngularJS service called $collection, which is a wrapper fo
| selector | [Mongo Selector (Object or String)](http://docs.meteor.com/#selectors) | Same as [Meteor Collection Find](http://docs.meteor.com/#find) | No |
| options | Object | Same as [Meteor Collection Find](http://docs.meteor.com/#find) | No |

The $collection service only has one method, and that is <code>bind</code>, which is used to bind the collection to an Angular model so that you can use it in your scope:
<code>bind</code>, which is used to bind the collection to an Angular model so that you can use it in your scope:

bind(scope, model, auto)

| Arguments | Type | Description | Required | Default |
| :------------ | :-------- | :------------------------------------------------------------------------ | :-------- | :-------- |
| scope | Scope | The scope the collection will be bound to. | Yes | |
| model | String | The model the collection will be bound to. | 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 |
| Arguments | Type | Description | Required | Default |
| :------------ | :--------------- | :------------------------------------------------------------------------ | :-------- | :-------- |
| scope | Scope | The scope the collection will be bound to. | Yes | |
| model | String | The model the collection will be bound to. | 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, bind 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 |

Once a collection has been bound using the <code>bind</code> method, the model will have access to the following methods for upserting/removing objects in the collection. If the <code>auto</code> argument as been set to true, then the user will not need to call these methods because these methods will be called automatically whenever the model changes.

Expand Down Expand Up @@ -145,6 +146,19 @@ For example:
| 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 |


### Subscribe

ngMeteor provides an AngularJS service called $subscribe, which is a wrapper for [Meteor.subscribe](http://docs.meteor.com/#meteor_subscribe) to subscribe the client to a Meteor.publish Method within AngularJS with promises.

$subscribe.subscribe(name, subscribeArguments)

Returns a promise when subscription is ready.

$subscribe.subscribe('todos').then(function(){
console.log($scope.todos);
});


### Adding controllers, directives, filters and services
It is best practice to not use globally defined controllers like they do in the AngularJS demos. Always use the exported package scope ngMeteor as your angular module to register your controller with $controllerProvider. Furthermore, to prevent errors when minifying and obfuscating the controllers, directives, filters or services, you need to use [Dependency Injection](http://docs.angularjs.org/guide/di). For example:

Expand Down
28 changes: 23 additions & 5 deletions modules/ngMeteor-collections.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';
var ngMeteorCollections = angular.module('ngMeteor.collections', []);
var ngMeteorCollections = angular.module('ngMeteor.collections', ['ngMeteor.subscribe']);

ngMeteorCollections.factory('$collection', ['$q', 'HashKeyCopier',
function ($q, HashKeyCopier) {
ngMeteorCollections.factory('$collection', ['$q', 'HashKeyCopier', '$subscribe',
function ($q, HashKeyCopier, $subscribe) {
return function (collection, selector, options) {
if (!selector) selector = {};
if (!(collection instanceof Meteor.Collection)) {
Expand All @@ -28,7 +28,7 @@ ngMeteorCollections.factory('$collection', ['$q', 'HashKeyCopier',
}
},

bind: function (scope, model, auto) {
bind: function (scope, model, auto, publisher) {
auto = auto || false; // Sets default binding type.
if (!(typeof auto === 'boolean')) { // Checks if auto is a boolean.
throw new TypeError("The third argument of bind must be a boolean.");
Expand Down Expand Up @@ -62,8 +62,26 @@ ngMeteorCollections.factory('$collection', ['$q', 'HashKeyCopier',
newItems.save(); // Saves all items.
}, auto);
}
}

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;
}
};
}
}
Expand Down
19 changes: 19 additions & 0 deletions modules/ngMeteor-subscribe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';
var ngMeteorSubscribe = angular.module('ngMeteor.subscribe', []);

ngMeteorSubscribe.service('$subscribe', ['$q',
function ($q) {
this.subscribe = function(name, subscribeArguments){
var deferred = $q.defer();

var subscription = Meteor.subscribe(name);

Deps.autorun(function() {
if ( subscription.ready() ) {
deferred.resolve();
}
});

return deferred.promise;
};
}]);
1 change: 1 addition & 0 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Package.on_use(function (api) {
// Lib Files
'lib/angular-hash-key-copier.js',
// Module Files
'modules/ngMeteor-subscribe.js',
'modules/ngMeteor-collections.js',
'modules/ngMeteor-template.js',
// Finally load ngMeteor File
Expand Down
1 change: 1 addition & 0 deletions urigo:ngmeteor.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Define ngMeteor and its dependencies
ngMeteor = angular.module('ngMeteor', [
'ngMeteor.subscribe',
'ngMeteor.collections',
'ngMeteor.template',
'hashKeyCopier'
Expand Down

0 comments on commit 3436e75

Please sign in to comment.