使用带有ember.js的编译模板时模型绑定错误

时间:2014-09-17 09:56:09

标签: ember.js

我正在尝试将ember模板放在模板文件夹下。
相关代码在这里:

define(["ember","holder","emberData","text!/templates/index.html"],function(Ember,Holder,DS,indexTpl){
window.App = Ember.Application.create();
/*Route definition*/
App.Router.map(function() {
    // put your routes here
});
App.IndexRoute = Ember.Route.extend({
  model: function() {
      return this.store.find("listItem");
  },
  setupController: function(controller, model) {
      this._super(controller, model);
      Ember.run.next(function (){Holder.run();});
  }
});
App.ApplicationView = Ember.View.extend({
    defaultTemplate:Ember.Handlebars.compile(indexTpl)
});
App.IndexController = Ember.ArrayController.extend({

});

/*Model definition*/
App.ListItem = DS.Model.extend({
    col1 : DS.attr("string"),
    col2 : DS.attr("string"),
    col3 : DS.attr("string"),
    col4 : DS.attr("string")
});
App.ApplicationAdapter = DS.FixtureAdapter.extend();
App.ListItem.reopenClass({
    FIXTURES:[...]
});
});

相关模板:

    ...
    {{#each}}
    <tr>
    <td>{{id}}</td>
    <td>{{col1}}</td>
    <td>{{col2}}</td>
    <td>{{col3}}</td>
    <td>{{col4}}</td>
    </tr>
    {{/each}}
    ...

在Chrome浏览器中测试结果:

    Uncaught Error: Assertion Failed: The value that #each loops over must be an Array. You passed '(null)' (wrapped in (generated application controller)) 

你能帮我解决这个问题吗?感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您正在将索引模板分配给应用程序视图。您的模板现在将期望应用程序模型在您尚未定义时成为数组。将索引模板分配给索引视图:

App.IndexView = Ember.View.extend({
    defulatTemplate: Ember.Handlebars.compile(indexTpl)
});