2015-11-03 59 views
0

繼從一本書的例子,我有我的js文件流星例如,對於新手

lists = new Mongo.Collection("Lists"); 

if (Meteor.isClient) { 
    Template.categories.helpers ({ 
    'Category': function(){ 
     return lists.find({}, {sort: {Category: 1}}); 
}}) 

,在我的HTML文件:

<body> 
<div id="categories-container"> 
    {{> categories}} 
</div> 
</body> 

<template name="categories"> 
<div class="title">my stuff</div> 
    {{#each lists}} 
    <div> 
     {{Category}} 
    </div> 
    {{/each}} 
</template> 

我輸入數據這些數據轉化爲列出了使用控制檯的字段使用類別:

> lists.insert({Category:"DVDs", items: {Name:"Mission Impossible" 
,Owner:"me",LentTo:"Alice"}}); 
> lists.insert({Category:"Tools", items: {Name:"Linear Compression 
Wrench",Owner:"me",LentTo: "STEVE"}}); 

但它不輸出數據。

+0

您是否安裝了* autopublish *軟件包?你的客戶如何從服務器訂閱出版物? –

回答

0

在您的示例中,您使用名稱類別有兩個不同的事情。
這是模板幫助函數的名稱,它返回列表項的數組
它是您列表中的字段項目的名稱。

執行{{#each Category}}幫手

each內返回的陣列上運行,你應該訪問{{Category}}{{item}}列表項目

0

你只需要打電話給助手lists代替的Category和您的代碼將工作:

Template.categories.helpers({ 
    lists: function() { 
    return lists.find({}, {sort: {Category: 1}}); 
    } 
}); 

{{#each lists}}表示:遍歷集合遊標(查找的結果)或通過調用幫助程序lists得到的數組。這裏的命名有點令人困惑,因爲lists也恰好是您正在迭代的集合的名稱。

each的內部,上下文是lists文檔。每個文檔都包含一個Category字段,您在div內輸出該字段。

推薦閱讀:

0

檢查運行命令:流星列表

它給你包的列表,這是你使用

如果autopublish包沒有要發佈,並在這裏訂閱您的數據列表的簡單的例子:

//in server 
Meteor.publish('lists',function(){ 
    return lists.find({}, {sort: {Category: 1}}); 
}) 
//in client 
Meteor.subscribe('lists'); 

對於更多Deatails:publish subscription

0

第一:該功能在你的「類」助手不應該引號之間(不知道這是否會引起問題)。

第二種:您正在使用{{#each}}循環錯誤。 您正在調用'Lists'集合,但您應該調用'Categories'幫助器。

<template name="categories"> 
<div class="title">my stuff</div> 
    {{#each Category}} 
    <div> 
     {{Category}} 
    </div> 
    {{/each}} 
</template> 

它非常混亂你的助手具有相同的名稱爲您在#each循環調用的領域。