2012-12-27 134 views
1

我正在測試Ember.js的主要功能。根據提供的指南,使用簡單的綁定和自動更新模板的以下代碼應輸出Hey there! This is My Ember.js Test Application!,但輸出Hey there! This is !Ember.js綁定和模板

JS:

// Create the application. 
var Application = Ember.Application.create(); 

// Define the application constants. 
Application.Constants = Ember.Object.extend({ 
    name: 'My Ember.js Test Application' 
}); 

// Create the application controller. 
Application.ApplicationController = Ember.Controller.extend(); 

// Create the application view. 
Application.ApplicationView = Ember.View.extend({ 
    templateName: 'application', 
    nameBinding: 'Application.Constants.name' 
}); 

// Create the router. 
Application.Router = Ember.Router.extend({ 
    root: Ember.Route.extend({ 
     index: Ember.Route.extend({ 
      route: '/' 
     }) 
    }) 
}) 

// Initialize the application. 
Application.initialize(); 

HBS:

<script type="text/x-handlebars" data-template-name="application"> 
    <h1>Hey there! This is <b>{{name}}</b>!</h1> 
</script> 

有什麼我做錯了嗎?

回答

2

正如您所指的是模板中的視圖屬性,您必須在view關鍵字前加上它。

所以儘量

<script type="text/x-handlebars" data-template-name="application"> 
    <h1>Hey there! This is <b>{{view.name}}</b>!</h1> 
</script> 

它應該工作。

哦,我忘了一些東西,綁定是錯誤的,你必須引用一個對象而不是一個類。嘗試

Application.constants = Ember.Object.create({ 
    name: 'My Ember.js Test Application' 
}); 

Application.ApplicationView = Ember.View.extend({ 
    templateName: 'application', 
    nameBinding: 'Application.constants.name' 
}); 
+0

不,我還是看到同樣的事情。這是沒有任何修改,我使用Ember.js網站提供的入門工具包。 –

+0

更新回答:) –

+0

是的,事實證明我的錯誤是使用'Ember.Object.extend'而不是'Ember.Object.create'。還有一件事,爲什麼Ember.js指南在處理欄變量時忽略了'view.'前綴? –