2012-08-10 82 views
1

這個非常簡單的應用程序無法正常工作。我的列表沒有顯示出來。爲什麼?我必須錯過一些關於流星如何工作的重要內容。爲什麼我不能用Meteor渲染這個簡單的列表?

recipes.html

<body> 
    <h3>Recipes</h3> 
    {{> recipes}} 
</body> 

<template name="recipes"> 
    <ul> 
     {{#each recipes}} 
      <li>{{name}}</li> 
     {{/each}} 
    </ul> 
</template> 

recipes.coffee

Recipes = new Meteor.Collection("recipes") 

if Meteor.is_client 
    Meteor.startup -> 
     Meteor.autosubscribe(-> 
      Meteor.subscribe('recipes') 
     ) 

     # THIS IS NOT WORKING 
     Template.recipes.recipes -> 
      return Recipes.find() 

if Meteor.is_server 
    Meteor.startup -> 
     if Recipes.find().count() == 0 
      data = [ 
       name: "Chocolate Chip Cookies" 
      , 
       name: "Spring Rolls" 
      ] 

     for item in data 
      Recipes.insert(item) 

    Meteor.publish('recipes', -> 
     return Recipes.find() 
    ) 

錯誤

Uncaught TypeError: Object function (data) { 
     var getHtml = function() { 
     return raw_func(data, { 
      helpers: partial, 
      partials: Meteor._partials 
     }); 
     }; 

     var react_data = { events: (name ? Template[name].events : {}), 
         event_data: data, 
         template_name: name }; 

     return Meteor.ui.chunk(getHtml, react_data); 
    } has no method 'recipes' 

我已經autopubl試過這種ish和沒有。我在這裏不瞭解什麼?

編輯:

我之前貼錯代碼,亞什德指出。現在的代碼是有問題的代碼。

回答

1

它不應該是:

Template.recipes.recipes = -> 
    return Recipes.find() 

,因爲1)你分配的功能Template.recipes.recipes和2)您通過模板recipes的列表recipes迭代。我猜你不需要用鑰匙recipes返回另一個對象。

+0

你說得對,但這只是我在發佈代碼時犯的一個錯誤。我編輯了這個問題,現在只顯示有問題的代碼。 – peter 2012-08-10 19:49:46

+1

'='呢?我認爲你需要它。這是在1)。 – 2012-08-11 06:27:23

0

應該是這樣的 -

Recipes = new Meteor.Collection("recipes") 

if Meteor.is_client 
    Meteor.startup -> 
     Meteor.autosubscribe(-> 
      Meteor.subscribe('recipes') 
     ) 

    # OUTINDENT 
    # ASSIGNMEMT, NOT FUNCTION CALL 
    Template.recipes.recipes = -> 
     return Recipes.find() 

if Meteor.is_server 
    Meteor.startup -> 
     if Recipes.find().count() == 0 
      data = [ 
       name: "Chocolate Chip Cookies" 
      , 
       name: "Spring Rolls" 
      ] 

      # INDENT 
      for item in data 
       Recipes.insert(item) 

    Meteor.publish('recipes', -> 
     return Recipes.find() 
    ) 
0
  1. 你需要確保你分配給Template.recipe.recipes(可能是你QN一個錯字)

    Template.recipes.recipes = -> 
        return Recipes.find() 
    
  2. 你不想在Meteor.startup中執行該操作,在模板被評估後運行(因此它會認爲Template.recipe.recipesnull) - 將其移動到喲的頂層我想這會很好。

相關問題