2

アレイ コントローラ変数にアクセスできません。たとえば、この単純なアプリケーションがあります。

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」をレンダリングするだけで、「hello tom」「hello tom」が期待されます。私が間違っているのは何ですか?

助けていただければ幸いです。

4

1 に答える 1

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}}
于 2013-10-23T05:22:58.077 に答える