断言失败:#each循环的值必须是Array Embers js

时间:2014-11-25 05:04:03

标签: javascript asp.net-mvc-4 ember.js handlebars.js

这是我的代码。

  <script type="text/x-handlebars">
        <h2>Welcome to Ember.js</h2>
        {{outlet}}
    </script>

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

和我的arraycontroller

App.IndexRoute = Ember.Route.extend({

});

App.IndexController = Ember.ArrayController.extend({

    model1: function () {
        return posts;
    } 

});

和我的Json

posts = [{
        title: "Raja",
        body: "There are lots of à la carte software environments in this world." }, {
        title: "Broken Promises",
        body: "James Coglan wrote a lengthy article about Promises in node.js." }];

请告诉我如果可以从控制器调用model1

1 个答案:

答案 0 :(得分:1)

是的,但它需要是一个属性,而不是一个函数

App.IndexController = Ember.ArrayController.extend({
    model1: function () {
        return posts;
    }.property()
});

但是如果它是一个模型,那么从路径返回它会更有意义,并在控制器中使用模型

App.IndexRoute = Ember.Route.extend({
  model: function(){
    return posts;
  }
});

App.IndexController = Ember.ArrayController.extend();

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