Ember.js控制器不起作用

时间:2013-10-23 03:40:40

标签: javascript ember.js controller ember-data

我无法访问我的数组控制器变量。我有这个简单的应用程序,例如:

App.js

App = Ember.Application.create();

App.ApplicationAdapter = DS.FixtureAdapter.extend();

App.Router.map(function() {
     this.route('hello');
});

App.IndexRoute = Ember.Route.extend({
      redirect: function() {
        this.transitionTo('hello');
      }
});

App.HelloController = Ember.ArrayController.extend({
     name: 'tom'
});

App.HelloRoute = Ember.Route.extend({
    model: function() {
           return this.store.find('hello');
     }
});

App.Hello = DS.Model.extend({
     title: DS.attr('string')
});

App.Hello.FIXTURES = [{
    id: 1,
    title: 'hello'
},{
    id: 2,
    title: 'hello'  
}];

我的观点是:

<script type="text/x-handlebars" data-template-name="application">
<h2>Welcome to Ember.js</h2>

{{outlet}}
</script> 


 <script type="text/x-handlebars" data-template-name="hello">
  {{#each}}
     {{title}} {{name}}
   {{/each}}
 </script>

当我保存这段代码时,页面只渲染“hello”“hello”,我希望“你好汤姆”“你好汤姆”。我做错了什么?

感谢帮助!

1 个答案:

答案 0 :(得分:2)

问题是您正在将每个语句中的上下文更改为模型,因此您不再在上下文中使用该名称。

http://emberjs.jsbin.com/UzUxIwa/1/edit

 <script type="text/x-handlebars" data-template-name="hello">
   {{#each item in controller}}
     {{item.title}} {{controller.name}}
   {{/each}}
 </script>

老实说甚至比使用itemController更好,并将属性放在该控制器上

http://emberjs.jsbin.com/UzUxIwa/2/edit

App.HelloController = Ember.ArrayController.extend({
  itemController:'singleHello'
});

App.SingleHelloController = Ember.ObjectController.extend({
  name: 'tom' 
});

{{#each item in controller}}
  {{item.title}} {{item.name}}</br>
{{/each}}