【问题标题】:Meteor subscriptions are not stopping after changing Ionic views更改 Ionic 视图后 Meteor 订阅不会停止
【发布时间】:2016-09-17 21:05:58
【问题描述】:

我正在使用 Angular-MeteorIonic

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 视图之间切换时如何正确停止订阅?

【问题讨论】:

  • 我认为您的“变通办法”实际上是正确的模式...

标签: javascript angularjs meteor ionic-framework angular-meteor


【解决方案1】:

Ionic 默认缓存控制器/视图,因此我的范围没有被破坏,因此在视图之间切换时不会调用 stop() 函数。

为了解决这个问题,我只需在$stateProvider 上设置cache: false,这就解决了我的问题。

但是,我确实喜欢我的视图被缓存,所以我想我会想出一个很好的方法来在视图之间切换时停止订阅但保持cache: true...欢迎任何优雅的想法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-10
    • 2018-01-24
    • 2014-12-15
    • 2017-07-13
    • 2018-09-19
    相关资源
    最近更新 更多