2014-11-08 57 views
4

流星來自測試版,我非常興奮使用它。我將它安裝在我的Windows機器上(從http://win.meteor.com/)並創建了一個應用程序。下面的代碼:無法讀取未定義的屬性'助手'

HTML:

<!-- simple-todos.html --> 
<head> 
    <title>Todo List</title> 
</head> 

<body> 
    <div class="container"> 
    <header> 
     <h1>Todo List</h1> 
    </header> 

    <ul> 
     {{#each tasks}} 
     {{> task}} 
     {{/each}} 
    </ul> 
    </div> 
</body> 

<template name="task"> 
    <li>{{text}}</li> 
</template> 

的javascript:

// simple-todos.js 
if (Meteor.isClient) { 
    // This code only runs on the client 
    Template.body.helpers({ 
    tasks: [ 
     { text: "This is task 1" }, 
     { text: "This is task 2" }, 
     { text: "This is task 3" } 
    ] 
    }); 
} 

這是完全相同的代碼作爲official meteor tutorial。如果我運行該示例,則標題呈現得很好。另一方面,該列表完全沒有出現。不知何故,幫手不工作。我收到以下javascript錯誤:

Uncaught TypeError: Cannot read property 'helpers' of undefined

在流星控制檯中,沒有打印錯誤或警告。我對流星非常興奮,我很樂意將它用於生產應用程序,所以請幫助我解決這個問題(可能非常簡單)。我希望這不僅是Windows中的一個問題(流星尚未正式發佈的Windows)。

+0

我首先要做的是加上'console.dir(模板) ;'嘗試調用'.helpers()'之前。問題在於'.body'是'undefined',因此找出'Template'的外觀是很有用的。 – Pointy 2014-11-08 16:11:22

回答

5

模板需要由<template name="xxx">定義。

<body>元素沒有幫助。

你可以使用一個全球性的幫手這一點,雖然:

Template.registerHelper("tasks", function() { 
    return [....] 
}); 

另一種方法是使用<template name="body">

<body> 
    {{>body}} 
</body> 

<template name="body"> 
    <div class="container"> 
    <header> 
     <h1>Todo List</h1> 
    </header> 

    <ul> 
     {{#each tasks}} 
     {{> task}} 
     {{/each}} 
    </ul> 
    </div> 
</template> 
+1

文檔不同意,使用'Template.body.helpers'應該可以正常工作。 https://docs.meteor.com/#/full/template_body – 2014-11-08 17:30:03

+0

@ PeppeL-G是的,你是對的。我認爲他們不贊成使用UI->模板轉換 – Akshat 2014-11-08 18:17:38

+1

所以我想這是流星的Windows版本的問題... – Pueggel 2014-11-09 17:13:34

相關問題