【问题标题】:Meteor Helper using query based on reactive variableMeteor Helper 使用基于反应变量的查询
【发布时间】:2018-06-27 09:05:59
【问题描述】:

我正在尝试使用一个帮助器,它应该返回一个 Collection,该 Collection 使用来自 templates:array 的反应数组指定整个 Collection 的子集 $in

我有

var tags = new ReactiveArray();

在某些事件中,我更改了数组的内容,类似于

tags.pushArray(note.tags);

(或者我应该使用.set()?)

我的助手是

Template.editor.helpers({
    tagslist() {
        return Tags.find({ _id: { $in : tags }});
    }, 
});

但是我在meteor.js:1010 中遇到了一个异常,看起来像这样

if (allArgumentsOfTypeString)
   console.log.apply(console, [Array.prototype.join.call(arguments, " ")]);

在堆栈中有compileValueSelector。这似乎表明 helper 的编译并不满足于它找到的内容。

我还尝试将tags 设为模板本地实例,并将.get() 添加到辅助查询中的tags。但结果相同。

我应该从哪里开始寻找?我正确使用 ReactiveArray 吗?是否可以做我想做的事,即基于 ReactiveArray 进行反应式查询?

【问题讨论】:

    标签: meteor helper


    【解决方案1】:

    我个人没有使用过 ReactiveArray,但我认为同样的模式也可以。我坚持使用 ReactiveVar,所以这里有一个例子可以让你朝着正确的方向前进。

    Template.editor.onCreated(function () {
        const instance = this;
        instance.tags = new ReactiveVar([]);
    });
    
    Template.editor.helpers({
        tagslist() {
            const tags = Template.instance().tags.get();
            return Tags.find({ _id: { $in : tags }});
        }
    });
    
    Template.editor.events({
        'click .tag'(event, instance){
            const tag = this;
            const tags = instance.tags.get();
            tags.push(tag);
            instance.tags.set(tags);
        }
    });
    

    【讨论】:

    • 回复:您对_id 的评论。 tags 变量包含我要检索的标签的 id:s。我对{ _id : { $in : tags }} 做错了吗?在 mongodb 中,它似乎做我想做的事......
    • 另外,我为tags 使用了模块范围的变量,而不是模板实例字段,因为我需要从模板外部调用“加载”代码。不行吗?
    • 您可以使用模块范围的变量,它的工作方式应该完全相同,但请记住,当页面导航发生时,您可能需要根据应用程序的工作方式清除其数据数组。
    • 我使它适用于这种模式。还没有使用 ReactiveArray 做任何进一步的努力。请务必阅读它。
    猜你喜欢
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    • 2017-10-26
    相关资源
    最近更新 更多