我设计了自己的模式来使用自定义 waitOn。您应该知道 IronRouter 不会呈现您的模板(因此它的任何助手都不会呈现,除非您手动调用它们,这通常很奇怪)除非 waitOn 函数中指定的每个句柄都准备好。
waitOn 是一种响应式计算,因此您指定的句柄应该是响应式数据源,并且随着它们的就绪状态的发展,waitOn 将自动重新评估,并最终通知 IronRouter 可以渲染模板。
因此,如果我们想将 waitOn 与订阅句柄以外的东西一起使用,我们必须使用响应式 ready() 方法(来自文档)实现我们自己的对象。我们称这个对象为“Waiter”,因为它的作用是等待某个事件发生,然后将其内部状态设置为就绪。
我将向您介绍一个解决常见问题的简单示例:图像预加载。假设您有一个模板渲染图像元素,其 src 属性存储在 Collection 中:您希望仅在客户端加载图像时渲染模板。
<template name="view">
<div>
<h1>{{title}}</h1>
<img src="{{firstImageUrl}}" />
<img src="{{secondImageUrl}}" />
</div>
</template>
我想出了以下界面:
this.route("view",{
path:"/view/:_id",
loadingTemplate:"loadingTemplate",
template:"view",
// our Waiter object handle designed to wait until images are loaded
imagePreloadingWaiter:new ImagePreloadingWaiter(),
// load is called only once each time the route is triggered
load:function(){
// reset our waiter
this.imagePreloadingWaiter.reset();
},
// before : reactive computation that will be rerun until the route template is rendered
before:function(){
// setup collection subscription
var subscriptionHandle=this.subscribe("collectionById",this.params._id);
if(subscriptionHandle.ready()){
// get the route data context
var collection=this.data();
// collect the images URLs we want to preload
var params={
images:[
collection.firstImageUrl,
collection.secondImageUrl
]
};
// fire the preloader
this.imagePreloadingWaiter.fire(params);
}
},
// we specify that we want to wait on our ImagePreloadingWaiter handle
waitOn:function(){
return this.imagePreloadingWaiter;
},
// return the data context used by this route
data:function(){
return Collection.findOne(this.params._id);
}
});
使用此路由定义,我们会显示加载模板,直到最终加载存储在集合中的图像 URL,这要归功于 waitOn 方法等待我们的 ImagePreloadingWaiter 接口句柄。
好的,现在我们对想要使用的接口有了一个概述,让我们实际实现它:
// Simple interface to use with the IronRouter waitOn method
Waiter=function(){
// avoid firing the waiter multiple time in a Deps.Computation context
this.isFired=false;
// reactive data source : have we been waiting long enough ?
this.isReady=false;
this.dependency=new Deps.Dependency();
};
_.extend(Waiter.prototype,{
// reset method, clear the waiter state
reset:function(){
this.isFired=false;
//
this.isReady=false;
this.dependency.changed();
},
// reactive ready method : this is the interface needed by waitOn
ready:function(){
this.dependency.depend();
return this.isReady;
},
// fire the Waiter object only once before being resetted
fire:function(params){
if(!this.isFired){
this.isFired=true;
// this abstract method must be overloaded in child classes
this.wait(params);
}
},
// must be called in Waiter.wait() to acknowledge we're done waiting
waitedEnough:function(){
// if we have reset the Waiter meanwhile, silently discard the notification
if(this.isFired){
this.isReady=true;
this.dependency.changed();
}
}
});
// Simple waiter that simply waits N seconds before getting ready
TimeoutWaiter=function(){
Waiter.call(this);
};
TimeoutWaiter.prototype=Object.create(Waiter.prototype);
_.extend(TimeoutWaiter.prototype,{
wait:function(params){
var self=this;
// after N seconds, notify that we are done waiting
Meteor.setTimeout(function(){
self.waitedEnough();
},params.seconds*1000);
}
});
// Image preloader for the IronRouter
ImagePreloadingWaiter=function(){
Waiter.call(this);
};
ImagePreloadingWaiter.prototype=Object.create(Waiter.prototype);
_.extend(ImagePreloadingWaiter.prototype,{
wait:function(params){
var self=this;
//
if(images.length>0){
var imageLoadedCounter=0;
_.each(images,function(imageUrl){
function onImageLoadOrError(){
imageLoadedCounter++;
if(imageLoadedCounter==images.length){
self.waitedEnough();
}
}
//
var image=$("<img/>");
image.load(onImageLoadOrError);
image.error(onImageLoadOrError);
image.prop("src",imageUrl);
});
}
else{
self.waitedEnough();
}
}
});
使用这个例子,我相信你会找到一个很好的解决方案来回答你的问题。
特别是,我认为您可能希望将“帮助”逻辑代码移动到 before IronRouter 钩子中。如果我的代码不清楚,请随时提出问题。