Skip to content

Commit 429a0ca

Browse files
committed
(subscriptions): Close when scope destroys also in $scope.MeteorCollection/Object.subscribe functions
Make $scope.MeteorObject.subscribe() and $scope.MeteorCollection.subscribe() automatically close subscription when $scope destroys
1 parent 0b99c5e commit 429a0ca

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

modules/angular-meteor-meteorCollection.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,15 +303,24 @@ angularMeteorCollections.factory('$meteorCollection', ['AngularMeteorCollection'
303303
}
304304
}]);
305305

306-
angularMeteorCollections.run(['$rootScope', '$q', '$meteorCollection',
307-
function($rootScope, $q, $meteorCollection) {
306+
angularMeteorCollections.run(['$rootScope', '$q', '$meteorCollection', '$meteorSubscribe',
307+
function($rootScope, $q, $meteorCollection, $meteorSubscribe) {
308308
Object.getPrototypeOf($rootScope).$meteorCollection = function() {
309309
var args = Array.prototype.slice.call(arguments);
310310
var collection = $meteorCollection.apply(this, args);
311+
var subscription = null;
312+
313+
collection.subscribe = function () {
314+
var args = Array.prototype.slice.call(arguments);
315+
subscription = $meteorSubscribe._subscribe(this, $q.defer(), args);
316+
return collection;
317+
};
311318

312319
this.$on('$destroy', function() {
313320
collection.stop();
314-
});
321+
if (subscription)
322+
subscription.stop();
323+
});
315324

316325
return collection;
317326
};

modules/angular-meteor-object.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,23 @@ angularMeteorObject.factory('$meteorObject', ['$rootScope', '$meteorUtils', 'Ang
143143
};
144144
}]);
145145

146-
angularMeteorObject.run(['$rootScope', '$q', '$meteorObject',
147-
function($rootScope, $q, $meteorObject) {
146+
angularMeteorObject.run(['$rootScope', '$q', '$meteorObject', '$meteorSubscribe',
147+
function($rootScope, $q, $meteorObject, $meteorSubscribe) {
148148
Object.getPrototypeOf($rootScope).$meteorObject = function() {
149149
var args = Array.prototype.slice.call(arguments);
150150
var object = $meteorObject.apply(this, args);
151+
var subscription = null;
152+
153+
object.subscribe = function () {
154+
var args = Array.prototype.slice.call(arguments);
155+
subscription = $meteorSubscribe._subscribe(this, $q.defer(), args);
156+
return object;
157+
};
151158

152159
this.$on('$destroy', function() {
153160
object.stop();
161+
if (subscription)
162+
subscription.stop();
154163
});
155164

156165
return object;

0 commit comments

Comments
 (0)