Skip to content

Commit 3436e75

Browse files
committed
Adding $subscribe to support subscription with promises and added auto subscribe option to $collection.bind
1 parent 3fe5bc7 commit 3436e75

File tree

5 files changed

+64
-11
lines changed

5 files changed

+64
-11
lines changed

README.md

+20-6
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,16 @@ ngMeteor provides an AngularJS service called $collection, which is a wrapper fo
4444
| selector | [Mongo Selector (Object or String)](http://docs.meteor.com/#selectors) | Same as [Meteor Collection Find](http://docs.meteor.com/#find) | No |
4545
| options | Object | Same as [Meteor Collection Find](http://docs.meteor.com/#find) | No |
4646

47-
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:
47+
<code>bind</code>, which is used to bind the collection to an Angular model so that you can use it in your scope:
4848

4949
bind(scope, model, auto)
5050

51-
| Arguments | Type | Description | Required | Default |
52-
| :------------ | :-------- | :------------------------------------------------------------------------ | :-------- | :-------- |
53-
| scope | Scope | The scope the collection will be bound to. | Yes | |
54-
| model | String | The model the collection will be bound to. | Yes | |
55-
| 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 |
51+
| Arguments | Type | Description | Required | Default |
52+
| :------------ | :--------------- | :------------------------------------------------------------------------ | :-------- | :-------- |
53+
| scope | Scope | The scope the collection will be bound to. | Yes | |
54+
| model | String | The model the collection will be bound to. | Yes | |
55+
| 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 |
56+
| 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 |
5657

5758
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.
5859

@@ -145,6 +146,19 @@ For example:
145146
| 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 |
146147

147148

149+
### Subscribe
150+
151+
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.
152+
153+
$subscribe.subscribe(name, subscribeArguments)
154+
155+
Returns a promise when subscription is ready.
156+
157+
$subscribe.subscribe('todos').then(function(){
158+
console.log($scope.todos);
159+
});
160+
161+
148162
### Adding controllers, directives, filters and services
149163
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:
150164

modules/ngMeteor-collections.js

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
2-
var ngMeteorCollections = angular.module('ngMeteor.collections', []);
2+
var ngMeteorCollections = angular.module('ngMeteor.collections', ['ngMeteor.subscribe']);
33

4-
ngMeteorCollections.factory('$collection', ['$q', 'HashKeyCopier',
5-
function ($q, HashKeyCopier) {
4+
ngMeteorCollections.factory('$collection', ['$q', 'HashKeyCopier', '$subscribe',
5+
function ($q, HashKeyCopier, $subscribe) {
66
return function (collection, selector, options) {
77
if (!selector) selector = {};
88
if (!(collection instanceof Meteor.Collection)) {
@@ -28,7 +28,7 @@ ngMeteorCollections.factory('$collection', ['$q', 'HashKeyCopier',
2828
}
2929
},
3030

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

66+
var deferred = $q.defer();
67+
68+
if (publisher) { // Subscribe to a publish method
69+
var publishName = null;
70+
if (publisher === true)
71+
publishName = collection._name;
72+
else
73+
publishName = publisher;
74+
75+
$subscribe.subscribe(publishName).then(function(){
76+
deferred.resolve(scope[model]);
77+
});
78+
79+
} else { // If no subscription, resolve immediately
80+
deferred.resolve(scope[model]);
81+
}
82+
83+
return deferred.promise;
84+
}
6785
};
6886
}
6987
}

modules/ngMeteor-subscribe.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
var ngMeteorSubscribe = angular.module('ngMeteor.subscribe', []);
3+
4+
ngMeteorSubscribe.service('$subscribe', ['$q',
5+
function ($q) {
6+
this.subscribe = function(name, subscribeArguments){
7+
var deferred = $q.defer();
8+
9+
var subscription = Meteor.subscribe(name);
10+
11+
Deps.autorun(function() {
12+
if ( subscription.ready() ) {
13+
deferred.resolve();
14+
}
15+
});
16+
17+
return deferred.promise;
18+
};
19+
}]);

package.js

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Package.on_use(function (api) {
2020
// Lib Files
2121
'lib/angular-hash-key-copier.js',
2222
// Module Files
23+
'modules/ngMeteor-subscribe.js',
2324
'modules/ngMeteor-collections.js',
2425
'modules/ngMeteor-template.js',
2526
// Finally load ngMeteor File

urigo:ngmeteor.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Define ngMeteor and its dependencies
22
ngMeteor = angular.module('ngMeteor', [
3+
'ngMeteor.subscribe',
34
'ngMeteor.collections',
45
'ngMeteor.template',
56
'hashKeyCopier'

0 commit comments

Comments
 (0)