From 3b1f4fddade1466a0cc8c1d501c69dd86eea56a7 Mon Sep 17 00:00:00 2001 From: maenriquez Date: Sun, 3 May 2015 14:01:52 -0600 Subject: [PATCH 1/2] - Changed $scope.subscribe to $scope.$meteorSubscribe to avoid possible conflicts - Added $scope.$meteorCollection & $scope.$meteorObject methods that will stop the collection or object on destruction of the scope --- modules/angular-meteor-meteorCollection.js | 14 +++++++ modules/angular-meteor-object.js | 14 +++++++ modules/angular-meteor-subscribe.js | 47 ++++++++++------------ 3 files changed, 50 insertions(+), 25 deletions(-) diff --git a/modules/angular-meteor-meteorCollection.js b/modules/angular-meteor-meteorCollection.js index 2750df8ab..30a8f245a 100644 --- a/modules/angular-meteor-meteorCollection.js +++ b/modules/angular-meteor-meteorCollection.js @@ -302,3 +302,17 @@ angularMeteorCollections.factory('$meteorCollection', ['AngularMeteorCollection' return ngCollection; } }]); + +angularMeteorCollections.run(['$rootScope', '$q', '$meteorCollection', + function($rootScope, $q, $meteorCollection) { + Object.getPrototypeOf($rootScope).$meteorCollection = function() { + var args = Array.prototype.slice.call(arguments); + var collection = $meteorCollection.apply(this, args); + + this.$on('$destroy', function() { + collection.stop(); + }); + + return collection; + }; + }]); diff --git a/modules/angular-meteor-object.js b/modules/angular-meteor-object.js index 90fee7fe8..a4f2787af 100644 --- a/modules/angular-meteor-object.js +++ b/modules/angular-meteor-object.js @@ -142,3 +142,17 @@ angularMeteorObject.factory('$meteorObject', ['$rootScope', '$meteorUtils', 'Ang return data; }; }]); + +angularMeteorObject.run(['$rootScope', '$q', '$meteorObject', + function($rootScope, $q, $meteorObject) { + Object.getPrototypeOf($rootScope).$meteorObject = function() { + var args = Array.prototype.slice.call(arguments); + var object = $meteorObject.apply(this, args); + + this.$on('$destroy', function() { + object.stop(); + }); + + return object; + }; + }]); diff --git a/modules/angular-meteor-subscribe.js b/modules/angular-meteor-subscribe.js index aa59caeef..1341a104e 100644 --- a/modules/angular-meteor-subscribe.js +++ b/modules/angular-meteor-subscribe.js @@ -3,51 +3,48 @@ var angularMeteorSubscribe = angular.module('angular-meteor.subscribe', []); angularMeteorSubscribe.service('$meteorSubscribe', ['$q', function ($q) { - this.subscribe = function(){ - var deferred = $q.defer(); - var args = Array.prototype.slice.call(arguments); + var self = this; + + this._subscribe = function(scope, deferred, args) { var subscription = null; - // callbacks supplied as last argument args.push({ - onReady: function () { + onReady: function() { deferred.resolve(subscription); }, - onError: function (err) { + onError: function(err) { deferred.reject(err); } }); - subscription = Meteor.subscribe.apply(this, args); + subscription = Meteor.subscribe.apply(scope, args); - return deferred.promise; + return subscription; }; - }]); -angularMeteorSubscribe.run(['$rootScope', '$q', - function($rootScope, $q) { - Object.getPrototypeOf($rootScope).subscribe = function(){ - var self = this; + this.subscribe = function(){ var deferred = $q.defer(); var args = Array.prototype.slice.call(arguments); var subscription = null; - // callbacks supplied as last argument - args.push({ - onReady: function () { - deferred.resolve(subscription); - }, - onError: function (err) { - deferred.reject(err); - } - }); + self._subscribe(this, deferred, args); + + return deferred.promise; + }; + }]); + +angularMeteorSubscribe.run(['$rootScope', '$q', '$meteorSubscribe', + function($rootScope, $q, $meteorSubscribe) { + Object.getPrototypeOf($rootScope).$meteorSubscribe = function() { + var deferred = $q.defer(); + var args = Array.prototype.slice.call(arguments); - subscription = Meteor.subscribe.apply(this, args); + var subscription = $meteorSubscribe._subscribe(this, deferred, args); - self.$on('$destroy', function() { + this.$on('$destroy', function() { subscription.stop(); }); return deferred.promise; }; -}]); \ No newline at end of file +}]); From 6dd3d93e57a16377f94729c07fc81844599d9fdf Mon Sep 17 00:00:00 2001 From: maenriquez Date: Sun, 3 May 2015 14:15:04 -0600 Subject: [PATCH 2/2] Updated docs to reflect new $scope.$meteorSubscribe, $scope.$meteorCollection, and $scope.$meteorObject methods --- .../client/views/api/api.meteorCollection.html | 8 ++++++-- .../angular-meteor/client/views/api/api.meteorObject.html | 8 ++++++-- .docs/angular-meteor/client/views/api/api.subscribe.html | 8 ++++---- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/.docs/angular-meteor/client/views/api/api.meteorCollection.html b/.docs/angular-meteor/client/views/api/api.meteorCollection.html index 6382c6d85..e6e68150d 100644 --- a/.docs/angular-meteor/client/views/api/api.meteorCollection.html +++ b/.docs/angular-meteor/client/views/api/api.meteorCollection.html @@ -8,16 +8,20 @@ {{#markdown}} -# $meteor.collection +# $meteor.collection / $scope.$meteorCollection A service that wraps the [Meteor collections](http://docs.meteor.com/#/full/collections) to enable reactivity within AngularJS. +Calling $scope.$meteorCollection will automatically stop the collection when the scope is destroyed. + ---- ## Usage $meteor.collection(collection, auto) + $scope.$meteorCollection(collection, auto) + ### Arguments @@ -104,7 +108,7 @@ }); // Bind without auto-save all todos to $scope.notAutoTodos - $scope.notAutoTodos = $meteor.collection(Todos, false).subscribe("publicTodos"); + $scope.notAutoTodos = $scope.$meteorCollection(Todos, false).subscribe("publicTodos"); // todo might be an object like this {text: "Learn Angular", sticky: false} // or an array like this: diff --git a/.docs/angular-meteor/client/views/api/api.meteorObject.html b/.docs/angular-meteor/client/views/api/api.meteorObject.html index c544c85bb..1f101c7b5 100644 --- a/.docs/angular-meteor/client/views/api/api.meteorObject.html +++ b/.docs/angular-meteor/client/views/api/api.meteorObject.html @@ -8,18 +8,22 @@ {{#markdown}} -# $meteor.object +# $meteor.object / $scope.$meteorObject A service that wraps a Meteor object to enable reactivity within AngularJS. Finds the first document that matches the selector, as ordered by sort and skip options. Wraps [collection.findOne](http://docs.meteor.com/#/full/findone) +Calling $scope.$meteorObject will automatically stop the object when the scope is destroyed. + ---- ## Usage $meteor.object(collection, selector, auto) + $scope.$meteorObject(collection, selector, auto) + ### Arguments @@ -97,7 +101,7 @@ $scope.party = $meteor.object(Parties, $stateParams.partyId); - $scope.partyNotAuto = $meteor.object(Parties, $stateParams.partyId, false); + $scope.partyNotAuto = $scope.$meteorObject(Parties, $stateParams.partyId, false); $scope.save = function() { $scope.partyNotAuto.save().then(function(numberOfDocs){ diff --git a/.docs/angular-meteor/client/views/api/api.subscribe.html b/.docs/angular-meteor/client/views/api/api.subscribe.html index 5f7bd2f4a..3ddcb83a3 100644 --- a/.docs/angular-meteor/client/views/api/api.subscribe.html +++ b/.docs/angular-meteor/client/views/api/api.subscribe.html @@ -8,12 +8,12 @@ {{#markdown}} -# $meteor.subscribe / $scope.subscribe +# $meteor.subscribe / $scope.$meteorSubscribe A service which is a wrapper for [Meteor.subscribe](http://docs.meteor.com/#/full/meteor_subscribe). It subscribes to a [Meteor.publish](http://docs.meteor.com/#/full/publishandsubscribe) method in the client and returns a [AngularJS promise](https://docs.angularjs.org/api/ng/service/$q) when ready. -Calling $scope.subscribe will automatically stop the subscription when the scope is destroyed. +Calling $scope.$meteorSubscribe will automatically stop the subscription when the scope is destroyed. ---- @@ -21,7 +21,7 @@ $meteor.subscribe(name, publisherArguments) - $scope.subscribe(name, publisherArguments) + $scope.$meteorSubscribe(name, publisherArguments) ### Arguments @@ -84,7 +84,7 @@ subscriptionHandle.stop(); }); - $scope.subscribe('books').then(function(subscriptionHandle){ + $scope.$meteorSubscribe('books').then(function(subscriptionHandle){ // Bind all the todos to $scope.books $scope.books = $meteor.collection(Books);