Meteor:在collectionHandle.ready()为true后,如何触发助手函数的reRun [英] Meteor: How to trigger reRun of helper function after collectionHandle.ready() is true

查看:238
本文介绍了Meteor:在collectionHandle.ready()为true后,如何触发助手函数的reRun的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的旧问题的新版本:



因此,感谢汤姆·科尔曼的帮助我终于找出了如何正确检查一个订阅是否准备好()或不。



我当前的代码结构看起来像这样:

  /client/app.js:

eventsHandle = null;
groupsHandle = null;
// ...
//第一个Deps.autorun():
//不依赖任何会话var,应该每次都运行
Deps.autorun (){
eventsHandle = Meteor.subscribe(events,function(){
console.log('Deps.autorun():Events loaded');
});
});

//第二个Deps.autorun():
//包含所有依赖于我的会话的订阅varehash
Deps.autorun(function(){
if(Session.get('ehash'))
groupsHandle = Meteor.subscribe(groups,Session.get('ehash'),function(){
console.log .autorun():加载了ehash的组:'+ Session.get('ehash'));
});
});
// ...

然后我查看特定的.js和.html文件所有模板内容在一个文件夹中调用:

  / client / views / 
- > < page> .js:

Template.x.dataLoaded = function(){
if(Session.get('ehash'))
if(eventsHandle&& ; groupsHandle&&& amp;&&&&&&&&> eventsHandle.ready()&&&  GroupHandle.ready()){
console.log('All data loaded!
singleevent = Events.find({ehash:Session.get('ehash')})。fetch()[0];
return true;
}
}

此助手 dataLoaded 基本上覆盖相应模板中的所有内容,并在 dataLoaded 返回true或显示加载微调器时显示内容。



问题是,在许多情况下,这不工作,因为这个dataLoaded代码只运行一次。因此,如果两个句柄没有准备好()在dataLoaded运行时,内容将永远不会出现。在这种情况下,我仍然看到所有的console.log来自app.js文件(Deps.autorun()东西),但日志所有数据加载!从来没有echod。



所以我的问题是:如何触发重新运行此代码,因此 dataLoaded

解决方案

该问题可以通过创建依赖关系简单地解决:

  var _dep = new Deps.Dependency 

template.x.dataLoaded = function(){
_dep.depend();
...
}


function handler(){
... do.stuff();
_dep.changed();
}

现在,每次运行 _dep.changed )方法,帮助程序将重新运行。简单!


This a new version of my old question:

So thanks to Tom Coleman's help I finally figured out on how to properly check if a subscription is ready() or not.

My current code structure looks like this:

/client/app.js:

eventsHandle = null;
groupsHandle = null;
// ...
// First Deps.autorun():
// Does not depend on any Session var, should just run every time
Deps.autorun(function() {
    eventsHandle = Meteor.subscribe("events", function() {
        console.log('Deps.autorun(): Events loaded');
    });
});

// Second Deps.autorun():
// contains all subscriptions which are dependent on my Session var "ehash"
Deps.autorun(function() {
    if(Session.get('ehash'))
        groupsHandle = Meteor.subscribe("groups", Session.get('ehash'), function() {
            console.log('Deps.autorun(): Groups loaded with ehash: ' + Session.get('ehash'));
        });
});
// ...

Then I have view specific .js and .html files for all the template stuff in a folder called:

/client/views/
--> <page>.js:

Template.x.dataLoaded = function() {
    if(Session.get('ehash'))
        if(eventsHandle && groupsHandle && eventsHandle.ready() && groupsHandle.ready()) {
            console.log('All data loaded!');
            singleevent = Events.find({ehash: Session.get('ehash')}).fetch()[0];
            return true;
        } 
}

This helper dataLoaded wraps basically everything in the corresponding template and shows the content when dataLoaded returns true or else shows a loading spinner.

Problem is that in many cases this does not work because this dataLoaded code is only run once. So if the two handles are NOT ready() at the time dataLoaded is run, content will NEVER show up. In this case I still see all the console.log's coming from the app.js file (the Deps.autorun() stuff) but the log "All data loaded!" is never echod.

So my question is: How do I trigger a rerun of this code so dataLoaded is run again so content will eventually show up?

best regards

解决方案

The problem can be solved simply by creating a dependency:

var _dep = new Deps.Dependency();

Template.x.dataLoaded = function() {
    _dep.depend();
    ...
}


function handler() {
    ... do.stuff();
    _dep.changed();
}

Now, every time you run _dep.changed() method, the helper will rerun. Simple!

这篇关于Meteor:在collectionHandle.ready()为true后,如何触发助手函数的reRun的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆