Ember Js为模型(类)赋予其子类的属性

时间:2013-12-13 20:03:48

标签: class ember.js model attributes subclass

我希望'category'中的每个模型显示其子类的{{workflowtype}}属性,'category /#'中的'workflow'。

这些是我得到的错误:

Assertion failed: The value that #each loops over must be an Array. You passed 

<VpcYeoman.Category:ember654:4> (wrapped in (generated category controller)) ember.js:417
Uncaught TypeError: Object [object Object] has no method 'addArrayObserver' 

这是我的代码。我把提到“工作流程”的代码放在最上面。

category.hbs

<h2>Category: {{permittype}}</h2><br>
<img class="department-add-icon" src="images/plus-blue.png">
<table class="table table-hover table-responsive sml-margin-left">
  <h4>Workflow Type</h4>
  {{input type="text" value=newWorkflowtype placeholder="Create a workflow and press enter" action="createWorkflow"}}
    <table>
    {{#each}}
      <tr {{bind-attr class="isCompleted:completed"}}>
        <td>
          <input type="checkbox" class="toggle">
          <label>{{workflowtype}}</label> 
        </td>
      </tr>
    {{/each}}
  </table>

VpcYeoman.CategoriesRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('category');
    return this.store.find('workflow');
  }
});

VpcYeoman.Category = DS.Model.extend({
    permittype: DS.attr('string'),
    isCompleted: DS.attr('boolean'),
    classNameBindings: ['isAdministrator']
});

VpcYeoman.Workflow = VpcYeoman.Category.extend({
    workflowtype: DS.attr('string')
})

VpcYeoman.Category.FIXTURES = [
  {
    id: 1,
    permittype:'Building'
  },
  {
    id: 2,
    permittype:'Electrical'
  },
  {
    id: 3,
    permittype:'Zoning'
  },
  {
    id: 4,
    permittype:'Fire'
  }
];

还好奇我如何在FIXTURES中显示类中子类的属性。

VpcYeoman.CategoriesController = Ember.ArrayController.extend({
  actions: {
    createCategory: function () {
      var permittype = this.get('newPermittype');
      if (!permittype.trim()) {return;}

      var category = this.store.createRecord('category', {
        permittype: permittype
      });

      this.set('newPermittype', '');

      category.save();
    },
    createWorkflow: function () {
      var workflowtype = this.get('newWorkflowtype');
      if (!workflowtype.trim()) {return;}

      var workflow = this.store.createRecord('workflow', {
        workflowtype: workflowtype
      });

      this.set('newWorkflowtype', '');

      workflow.save();
    }
  }
});

    <h2>New Category</h2> 
      {{input type="text" value=newPermittype placeholder="Create a category and press enter" action="createCategory"}}
  <table>
  {{#each}}
    <tr {{bind-attr class="isCompleted:completed"}}>
      <td>
        <input type="checkbox" class="toggle">
        <label>{{#linkTo 'category' this}}{{permittype}}{{/linkTo}}</label> 
      </td>
    </tr>
  {{/each}}
</table>

VpcYeoman.Router.map(function () {
  this.resource('categories', { path: '/categories' });
  this.resource('category', { path: '/category/:category_id' });
});

1 个答案:

答案 0 :(得分:1)

您的类别路线是单个对象,因此您无法使用每个对象进行迭代。

此钩子中的第二行也从未被击中,

VpcYeoman.CategoriesRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('category');
    return this.store.find('workflow');
  }
});

如果您希望工作流可用,可以在setupController

期间将其挂钩
VpcYeoman.CategoriesRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('category');
  },
  setupController:function(controller, model){
    this._super(controller, model);
    controller.set('workflows', this.store.find('workflow'));
  }
});

然后

{{#each workflows}}
  <tr {{bind-attr class="isCompleted:completed"}}>
    <td>
      <input type="checkbox" class="toggle">
      <label>{{workflowtype}}</label> 
    </td>
  </tr>
{{/each}}