代码之家  ›  专栏  ›  技术社区  ›  thoni56

基于反应变量的流星辅助查询

  •  0
  • thoni56  · 技术社区  · 6 年前

    我试图使用一个助手,该助手应返回一个集合,指定整个集合的一个子集 $in 使用来自 templates:array 是的。

    我有

    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() 标签 在helper查询中。但结果是一样的。

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

    1 回复  |  直到 6 年前
        1
  •  0
  •   Derrick Gremillion    6 年前

    我个人并没有使用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);
        }
    });