{{#each model}}未列出模型

时间:2013-12-10 17:55:26

标签: javascript templates model-view-controller ember.js model

我正在尝试显示每个部门的名称。我手工制作了一个“部门”模型,基于我制作的另一个模型。尽管它们是相同的,但#each不会遍历“部门”并列出它们。

departments.hbs>

{{#each model}}
 <tr>
  <td>
  {{#linkTo 'department' this}}{{this.departmentName}}{{/linkTo}}
  </td>
  <td>{{this.departmentName}}</td>
 </tr> 
{{/each}}

没有错误。它只是没有列出部门。

VpcYeoman.DepartmentsView = Ember.View.extend({
  templateName: 'departments'});

VpcYeoman.DepartmentView = Ember.View.extend({
  templateName: 'department'
});

VpcYeoman.DepartmentsController = Ember.ObjectController.extend({
  // Implement your controller here.
});

VpcYeoman.Department = DS.Model.extend({
  departmentName: DS.attr('string'),
  departmentMembers: DS.attr('string')
});

VpcYeoman.Department.reopen({
  // certainly I'm duplicating something that exists elsewhere...
  attributes: function(){
      var attrs = [];
      var model = this;
      Ember.$.each(Ember.A(Ember.keys(this.get('data'))), function(idx, key){
        var pair = { key: key, value: model.get(key) };
        attrs.push(pair);
      });
      return attrs;
    }.property()
});

VpcYeoman.Department.FIXTURES = [

  {
    id: 0,
    departmentName: "Sickness",
    departmentMembers: "61"
  },

  {
    id: 1,
    departmentName: "Health",
    departmentMembers: "69"
  }

];

'department /#/'工作。为什么{{#each model}}无法找到部门列表?

编辑:

VpcYeoman.DepartmentsController = Ember.ArrayController.extend({
  // Implement your controller here.
});

在{{#each model)循环之前输入{{log model}}后,我得到了这个回复:

    [nextObject: function, firstObject: undefined, lastObject: undefined, contains: function, getEach: function…]
    __ember1386699686611_meta: Meta
    length: 0
    __proto__: Array[0]

VpcYeoman.DepartmentsRoute = Ember.Route.extend({
    renderTemplate: function() {
    this.render();
  }
});

VpcYeoman.DepartmentRoute = Ember.Route.extend({});

2 个答案:

答案 0 :(得分:3)

您需要使用以下内容声明DepartmentsRoute

VpcYeoman.DepartmentsRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('department');
  }
});

答案 1 :(得分:2)

DepartmentsController可能应该是一个ArrayController,您可以在控制台中查看该模型,以便在每个

之前使用((log model))验证它是否具有某些内容

您需要实现模型钩子,返回部门

VpcYeoman.DepartmentsRoute = Ember.Route.extend({
  model: function(){
    return this.store.find('department');
  },
  renderTemplate: function() {
    this.render();
  }
});

部门路线根据路线名称猜测并实施默认模型钩子。