2012-08-10 84 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) - 将其移动到哟的顶层我想这会很好。

相关问题