如何处理依赖于帮助程序结果的值?

时间:2016-07-11 18:20:31

标签: meteor angular-meteor

所以我有一个带有流星辅助方法的Angular控制器,如下所示。

function localeCtrl($scope, $reactive, $stateParams{
    $reactive(this).attach($scope);
    var self = this;
    self.helpers({
        locale: function(){ return Locales.findOne($stateParams.id)},
        staff: function(){
            // Load data from second collection based on current Locale.
            // But how?
        },
        address: function(){
            // Take self.location.address and massage it to provide
            // google maps link.  How?
        }
        tags: function(){
            // Collect all unique instances of a given tag by
            // iterating over the available locales.
            // E. G. If 10 locales have the 'restaurant' tag, and 5
            // more have the 'library' tag, I want an array of
            // ['restaurant', 'library'] -- easy enough to do
            // by iterating over the locales, but how do I do that
            // reactively?
        }
    });
}

不幸的是,我需要在locale()获取的数据上设置基于的其他属性。我初始化控制器时无法设置它们,因为locale()中的值随着从服务器获取数据而更改。但我需要访问区域设置中的数据,例如,创建谷歌地图地址,或获取相关记录。 (由于当时我确定有意义的原因,它们没有嵌入到语言环境文档中。)

编辑:

此外,我正在使用地面数据库存储数据的本地副本以供离线访问,这使生活变得更加复杂。

1 个答案:

答案 0 :(得分:1)

Probably you best bet is to publish your collection using publishComposite which is implemented using the reywood:publish-composite package.

Add the package:

meteor add reywood:publish-composite

Now where you publish the Locales collection you would do something like this:

Meteor.publishComposite('locales', function() {
 return {
    find() {
      //Put whatever you need in the query for locales
      const query = {
        _userId: this.userId
      };


      return Locales.find(query);
    },

    children: [{
      find(locale) {
        return Staff.find({ localeId: locale._id });
      }
    }]
  };
});

Then in your controller before the helper you add this:

this.subscribe('locales');

Now you should be able to simply call the code like this:

 this.helpers({
        locale(){
            return Locales.findOne(this.$stateParams.id);
        }
    });

And access it in the template like this:

 locale.staff

Give that a try and let me know!