如何使Meteor助手不具有响应性?

8

我想让这段代码不具有响应性。有没有办法?

Template.foo.helpers({
    info: function(){
        var user = Meteor.user();
            if (user && user.profile)
                return user.profile.info;
    }
});

我知道当你使用Foo.find({}, {reactive:false})时有一种方式。

我在想是否有一个等价的方法。

2个回答

14
我认为你要找的是 这里 描述的 Tracker.nonreactive(func) 函数。根据文档,你需要传递一个函数给该函数以执行,并且该函数的结果将被该函数返回。此外,此函数将不会关注你自己定义的函数中的任何响应式数据源更新。
我建议像这样重写你的 helper 函数:
Template.foo.helpers({
    info: function() {
        return Tracker.nonreactive(function() {
            var user = Meteor.user();
            if(user && user.profile) {
                return user.profile.info;
            } else {
                // return some other appropriate value if the if-statement above
                // is not fulfilled
            }
        });
    }
});

2
您正在寻找 Tracker.nonreactive(不好意思,我的手机上回答得很差)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接