【问题标题】:Meteor Reapply Collection Transform流星重新应用集合转换
【发布时间】:2013-03-10 08:41:12
【问题描述】:

在最新版本的 Meteor 中,添加了变换功能。

示例用法:

var myCollection = new Meteor.Collection("mycollection",
{
    transform: function(doc){ 
        doc["newProperty"] = "test"; return doc; 
    })
}

有没有办法重新计算这些转换?

我在 DOM 中使用时间人性化功能(MomentJS 人性化),这实际上是对集合进行的唯一转换,因此每 10 秒重新应用一次(大约 15 个条目) 应该不会对性能造成太大影响。

【问题讨论】:

    标签: collections meteor


    【解决方案1】:

    一种方法是将您的收集结果放入Dependency

    客户端 JS:

    var times = [];
    var timesDeps = new Deps.Dependency;
    
    var getTimes = function () {
       Deps.depend(timesDeps);
       return myCollection.find();  //Your Query
    };
    
    
    Template.home.times = function() {
        return getTimes();
    }
    
    Meteor.setInterval(function() {
        timesDeps.changed();
    }, 10000) //Recalculate ever 10000 ms
    

    因此,您的集合正在使用 getTimes() 调用,当您调用 timesDeps.changed() 时,它的反应上下文无效并刷新数据,从而再次调用转换。

    【讨论】:

    • 嗯,我认为转换不是反应式的?
    • 转换确实是反应式的。每次find() 应用的转换函数都会在数据更新时调用反应性方法,因此这里我每 10 秒手动使timesDeps 内的现有反应性上下文无效
    猜你喜欢
    • 2016-08-08
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多