【问题标题】:How do I handle values that are dependent on the result of a helper?如何处理依赖于助手结果的值?
【发布时间】:2016-07-13 01:19:01
【问题描述】:

所以我有一个带有流星辅助方法的 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() 中的值会随着从服务器获取数据而改变。但我需要访问语言环境中的数据,例如,创建谷歌地图地址或获取相关记录。 (出于我当时确信的原因,它们没有嵌入语言环境文档中)。

编辑:

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

【问题讨论】:

    标签: meteor angular-meteor


    【解决方案1】:

    您最好的选择可能是使用publishComposite 发布您的收藏,这是使用reywood:publish-composite 包实现的。

    添加包:

    meteor add reywood:publish-composite
    

    现在您在发布 Locales 集合的位置执行以下操作:

    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 });
          }
        }]
      };
    });
    

    然后在你的控制器之前你添加这个:

    this.subscribe('locales');
    

    现在您应该可以像这样简单地调用代码:

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

    并像这样在模板中访问它:

     locale.staff
    

    试试看,然后告诉我!

    【讨论】:

    • 不幸的是,一些关键逻辑是倒退的。 (别怪我,我是在开发后期才来的)。员工 ID 存储在区域设置中。 (编辑:实际上,我想我现在知道如何调整了,在第二次阅读时)另一个问题是我正在使用地面数据库进行离线模式,我不确定这是否与你的建议相配合。一旦我修复了一些其他问题,我们将看看它是如何工作的。
    • 另外,我添加了一个我认为您的建议涵盖的其他用例问题。我认为应该像只包含转换逻辑的“子”方法一样简单……对吗?
    • 我认为您应该能够添加另一个子集合并像对待工作人员一样简单地引用它。我会查看我引用的链接的文档来确定。听起来你走在正确的道路上。 :)
    • 我正在用另一个用例问题再次更新问题。
    • 您是否尝试过使用 publishComposite?该模式应该可以解决您的大部分问题。带有标签的另一部分可以在 mongoDB 中创建为聚合项。如果您访问 reywood 示例(请参阅上面的链接),它会为您提供示例。关注 topTenPosts 和标签类似。
    猜你喜欢
    • 2019-04-30
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多