2012-12-27 44 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.'前缀? –