更改 Ionic 视图后,Meteor 订阅不会停止
Meteor subscriptions are not stopping after changing Ionic views
我正在使用 Angular-Meteor with Ionic。
Meteor 版本:1.3.2.4
,Angular 版本:1.5.5
,Ionic 版本:1.2.4
.
我的 Meteor/Angular 应用也在使用 ES2015。
对于一种观点,我有:
import angular from 'angular';
import angularMeteor from 'angular-meteor';
import uiRouter from 'angular-ui-router';
import { Meteor } from 'meteor/meteor';
import { SomeCollection } from 'path/to/collection';
import './view1.html';
class View1 {
constructor($scope, $reactive, $state) {
this.$state = $state;
this.$scope = $scope;
$reactive(this).attach($scope);
this.subscribe('my.subscription');
this.helpers({
list: () => {
return SomeCollection.find({});
}
});
}
}
export default angular.module('view1', [
angularMeteor,
uiRouter,
View1
]).config(config);
function config($stateProvider) {
'ngInject';
$stateProvider.state('app.view1', {
url: '/view1',
template: '<view-1></view-1>',
views: {
'appContent': {
controller: View1,
controllerAs: 'view1',
templateUrl: 'path/to/template.html'
}
}
});
}
另一个视图我有类似的设置,只是我订阅了不同的出版物。
类似于:
import angular from 'angular';
import angularMeteor from 'angular-meteor';
import uiRouter from 'angular-ui-router';
import { Meteor } from 'meteor/meteor';
import { SomeOtherCollection } from 'path/to/other/collection';
import './view2.html';
class View2 {
constructor($scope, $reactive, $state) {
this.$state = $state;
this.$scope = $scope;
$reactive(this).attach($scope);
this.subscribe('my.other.subscription');
this.helpers({
list: () => {
return SomeOtherCollection.find({});
}
});
}
}
export default angular.module('view2', [
angularMeteor,
uiRouter,
View2
]).config(config);
function config($stateProvider) {
'ngInject';
$stateProvider.state('app.view2', {
url: '/view2',
template: '<view-2></view-2>',
views: {
'appContent': {
controller: View2,
controllerAs: 'view2',
templateUrl: 'path/to/other/template.html'
}
}
});
}
当我在 Ionic 视图之间切换时,我注意到我仍在从之前视图的订阅中进行轮询。根据文档,如果我使用 $scope
,应该会自动调用 stop()
函数。
但是,我可以看到使用 Mongol 我的订阅不会停止切换视图。
我目前已经能够使用快速修复,但它很丑陋并且迫使我用我认为没有必要的变通办法填充我的代码,即做:
$scope.$on('$ionicView.enter', () => {
this.subscriptions = [];
this.subscriptions.push(this.subscribe('some.collection'));
this.subscriptions.push(this.subscribe('some.other.collection'));
});
$scope.$on('$ionicView.leave', () => {
for (var sub in this.subscriptions) {
this.subscriptions[sub].stop();
}
});
我做错了什么?在 Ionic 视图之间切换时如何正确停止订阅?
默认情况下 Ionic 缓存 controllers/views,所以我的范围没有被破坏,因此在视图之间切换时没有调用 stop()
函数。
为了解决这个问题,我只需在 $stateProvider
上设置 cache: false
,这就解决了我的问题。
但是,我确实认为我喜欢我的视图被缓存,所以我想我会想出一个很好的方法来在视图之间切换时停止订阅,但保持 cache: true
...任何优雅的想法都是欢迎。
我正在使用 Angular-Meteor with Ionic。
Meteor 版本:1.3.2.4
,Angular 版本:1.5.5
,Ionic 版本:1.2.4
.
我的 Meteor/Angular 应用也在使用 ES2015。
对于一种观点,我有:
import angular from 'angular';
import angularMeteor from 'angular-meteor';
import uiRouter from 'angular-ui-router';
import { Meteor } from 'meteor/meteor';
import { SomeCollection } from 'path/to/collection';
import './view1.html';
class View1 {
constructor($scope, $reactive, $state) {
this.$state = $state;
this.$scope = $scope;
$reactive(this).attach($scope);
this.subscribe('my.subscription');
this.helpers({
list: () => {
return SomeCollection.find({});
}
});
}
}
export default angular.module('view1', [
angularMeteor,
uiRouter,
View1
]).config(config);
function config($stateProvider) {
'ngInject';
$stateProvider.state('app.view1', {
url: '/view1',
template: '<view-1></view-1>',
views: {
'appContent': {
controller: View1,
controllerAs: 'view1',
templateUrl: 'path/to/template.html'
}
}
});
}
另一个视图我有类似的设置,只是我订阅了不同的出版物。
类似于:
import angular from 'angular';
import angularMeteor from 'angular-meteor';
import uiRouter from 'angular-ui-router';
import { Meteor } from 'meteor/meteor';
import { SomeOtherCollection } from 'path/to/other/collection';
import './view2.html';
class View2 {
constructor($scope, $reactive, $state) {
this.$state = $state;
this.$scope = $scope;
$reactive(this).attach($scope);
this.subscribe('my.other.subscription');
this.helpers({
list: () => {
return SomeOtherCollection.find({});
}
});
}
}
export default angular.module('view2', [
angularMeteor,
uiRouter,
View2
]).config(config);
function config($stateProvider) {
'ngInject';
$stateProvider.state('app.view2', {
url: '/view2',
template: '<view-2></view-2>',
views: {
'appContent': {
controller: View2,
controllerAs: 'view2',
templateUrl: 'path/to/other/template.html'
}
}
});
}
当我在 Ionic 视图之间切换时,我注意到我仍在从之前视图的订阅中进行轮询。根据文档,如果我使用 $scope
,应该会自动调用 stop()
函数。
但是,我可以看到使用 Mongol 我的订阅不会停止切换视图。
我目前已经能够使用快速修复,但它很丑陋并且迫使我用我认为没有必要的变通办法填充我的代码,即做:
$scope.$on('$ionicView.enter', () => {
this.subscriptions = [];
this.subscriptions.push(this.subscribe('some.collection'));
this.subscriptions.push(this.subscribe('some.other.collection'));
});
$scope.$on('$ionicView.leave', () => {
for (var sub in this.subscriptions) {
this.subscriptions[sub].stop();
}
});
我做错了什么?在 Ionic 视图之间切换时如何正确停止订阅?
默认情况下 Ionic 缓存 controllers/views,所以我的范围没有被破坏,因此在视图之间切换时没有调用 stop()
函数。
为了解决这个问题,我只需在 $stateProvider
上设置 cache: false
,这就解决了我的问题。
但是,我确实认为我喜欢我的视图被缓存,所以我想我会想出一个很好的方法来在视图之间切换时停止订阅,但保持 cache: true
...任何优雅的想法都是欢迎。