diff --git a/README.md b/README.md
index c9f90c4b4..778d3ea9f 100644
--- a/README.md
+++ b/README.md
@@ -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 bind
, which is used to bind the collection to an Angular model so that you can use it in your scope:
+bind
, 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 bind
method, the model will have access to the following methods for upserting/removing objects in the collection. If the auto
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.
@@ -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:
diff --git a/modules/ngMeteor-collections.js b/modules/ngMeteor-collections.js
index 929e54660..5bee4c447 100644
--- a/modules/ngMeteor-collections.js
+++ b/modules/ngMeteor-collections.js
@@ -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)) {
@@ -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.");
@@ -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;
+ }
};
}
}
diff --git a/modules/ngMeteor-subscribe.js b/modules/ngMeteor-subscribe.js
new file mode 100644
index 000000000..ba0360859
--- /dev/null
+++ b/modules/ngMeteor-subscribe.js
@@ -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;
+ };
+ }]);
\ No newline at end of file
diff --git a/package.js b/package.js
index fe5460537..b47e2af58 100644
--- a/package.js
+++ b/package.js
@@ -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
diff --git a/urigo:ngmeteor.js b/urigo:ngmeteor.js
index b66bee5d7..4b098f74c 100644
--- a/urigo:ngmeteor.js
+++ b/urigo:ngmeteor.js
@@ -1,5 +1,6 @@
// Define ngMeteor and its dependencies
ngMeteor = angular.module('ngMeteor', [
+ 'ngMeteor.subscribe',
'ngMeteor.collections',
'ngMeteor.template',
'hashKeyCopier'