Javascript 使用angular meteor管理ui路由中的订阅

Javascript 使用angular meteor管理ui路由中的订阅,javascript,angularjs,meteor,Javascript,Angularjs,Meteor,我目前正在使用angularJS和meteor构建一个应用程序。我现在在问自己,如何正确处理订阅。我目前的做法是: 在每个从属订阅就绪时解析路由 确保已停止以前对该发布的订阅 有没有更有效的方法 profileRoutes.js: function profileRoutes($stateProvider, ResolverProvider) { $stateProvider .state('user.profile', { url: '/:username/prof

我目前正在使用angularJS和meteor构建一个应用程序。我现在在问自己,如何正确处理订阅。我目前的做法是:

  • 在每个从属订阅就绪时解析路由
  • 确保已停止以前对该发布的订阅
有没有更有效的方法

profileRoutes.js:

function profileRoutes($stateProvider, ResolverProvider) {
  $stateProvider
    .state('user.profile', {
      url: '/:username/profile',
      resolve: _.extend(
        {
          $title: ['$stateParams', function($sp) {
            return $sp.username + "'s Profil";
          }]
        },
        ResolverProvider.waitForMedia(),
        ResolverProvider.waitForUsers()
      ),
      abstract: true,
      views: {
        'main': {
          controller: 'UserProfileController',
          templateUrl: 'client/components/users/profile.html'
        }
      }
    });
  }

  angular
    .module('myApp')
    .config(profileRoutes);
resolver.js

function ResolverProvider() {

  /**
   * _stopIfSubscribed
   *
   * Checks if a subscription for that publication is already present and stops it.
   * @param publicationName
   * @private
   */
   function _stopIfSubscribed(publicationName) {
     _.forEach(_.get(Meteor, 'default_connection._subscriptions'), (handle) => {
     if(_.get(handle, 'name') === publicationName && handle.stop) {
       handle.stop();
     }
   });
 }

 /**
  * waitForUsers
  *
  * Returns resolvers for waiting on my own and all other users
  * Does not require a user
  * @returns {{meSubscription: *[], usersSubscription: *[]}}
  */
 this.waitForUsers = () => {
   return {
     "meSubscription": ['$meteor', function ($meteor) {
       return $meteor.waitForUser();
     }],
      "usersSubscription": ['$meteor', function ($meteor) {
       _stopIfSubscribed('allUsers');
       return $meteor.subscribe('allUsers');
     }]
   };
 };

 /**
  * waitForMedia
  *
  * Returns resolvers for waiting on galleries and media
  * @returns {{mediaSubscription: *[], gallerySubscription: *[]}}
  */
 this.waitForMedia = () => {
   return {
     "mediaSubscription": ['$meteor', function ($meteor) {
       _stopIfSubscribed('media');
       return $meteor.subscribe('media');
     }],
     "gallerySubscription": ['$meteor', function ($meteor) {
       _stopIfSubscribed('galleries');
       return $meteor.subscribe('galleries');
     }]
   };
 };
}