यह मैं कैसे आगे बढ़ना है:
client/views/home/home.html
<template name="home">
{{#if itemsReady}}
{{> itemsList}}
{{/if}}
</template>
<template name="itemsList">
<ul>
{{#each items}}
{{> item}}
{{/each}}
</ul>
</template>
<template name="item">
<li>{{value}}</li>
</template>
client/views/home/home.js
Template.home.helpers({
itemsReady:function(){
return Meteor.subscribe("items").ready();
}
});
Template.itemsList.helpers({
items:function(){
return Items.find();
}
});
Template.itemsList.rendered=function(){
// will output 100, once
console.log(this.$("li").length);
};
lib/collections/items.js
Items=new Mongo.Collection("items");
server/collections/items.js
insertItems=function(){
var range=_.range(100);
_.each(range,function(index){
Items.insert({value:"Item "+index});
});
};
Meteor.publish("items",function(){
return Items.find();
});
server/startup.js
Meteor.startup(function(){
Items.remove({});
if(Items.find().count()===0){
insertItems();
}
});
हम निर्दिष्ट है कि हम, आइटम की हमारी सूची को रखना चाहते हैं केवल जब प्रकाशन के लिए तैयार है तो द्वारा उस समय डेटा उपलब्ध है और li
तत्वों की सही संख्या मिल जाएगा सूची में प्रदर्शित कॉलबैक में प्रदर्शित।
अब एक ही का उपयोग कर
iron:router
waitOn
सुविधा:
client/views/home/controller.js
HomeController=RouteController.extend({
template:"home",
waitOn:function(){
return Meteor.subscribe("items");
}
});
client/lib/router.js
Router.configure({
loadingTemplate:"loading"
});
Router.onBeforeAction("loading");
Router.map(function(){
this.route("home",{
path:"/",
controller:"HomeController"
});
});
client/views/loading/loading.html
<template name="loading">
<p>LOADING...</p>
</template>
iron:router
का उपयोग करना संभवतः बेहतर है क्योंकि यह एक सामान्य पैटर्न को सुन्दर तरीके से हल करता है: हमें आइटम की आवश्यकता नहीं है, अब और अधिक पढ़ें, होम टेम्पलेट केवल तभी प्रदान किया जाएगा जब WaitList
प्रतीक्षा करके लौटाया जाएगा, वैश्विक रूप से तैयार हो जाएगा।
किसी को एक लोडिंग टेम्पलेट जोड़ने और डिफ़ॉल्ट "लोडिंग" हुक सेट अप करना न भूलना अन्यथा यह काम नहीं करेगा।
क्या आप कहीं डेटा की सदस्यता लेते हैं? क्या आप लौह राउटर का उपयोग कर रहे हैं? –
'सूची इटैम' टेम्पलेट्स संभावित रूप से प्रतिक्रियाशील संग्रह से डेटा प्राप्त करते हैं, इस मामले में आप कभी भी यह सुनिश्चित नहीं कर सकते कि उनमें से सभी * प्रस्तुत किए गए हैं। प्रारंभिक सेट तैयार होने पर आप अधिसूचना प्राप्त करने के लिए कुछ युक्तियों का उपयोग कर सकते हैं। फिर भी, आप शायद "उल्का रास्ता" में कुछ नहीं करने की कोशिश कर रहे हैं, इसलिए मैं एक बेहतर समाधान की तलाश में हूं। आप क्या हासिल करने का प्रयास कर रहे हैं? –