0

我有两个对象如下:

AC.Category = DS.Model.extend({
  name: DS.attr('string'),
  order: DS.attr('number'),
  subcats: DS.hasMany('AC.SubCategory')
});

AC.SubCategory = DS.Model.extend({
  name: DS.attr('string'),
  order: DS.attr('number'),
  category: DS.belongsTo('AC.Category')
});

我正在尝试通过我的 IndexRoute 按顺序(通过它们的“顺序”属性)输出所有类别。所以代码看起来像这样:

AC.IndexRoute = Ember.Route.extend({
  model: function() {
    return AC.Category.find();
  }
});

AC.IndexController = Ember.ArrayController.extend({
  sortProperties: ['order'],
  sortAscending: true
});

这对顶级类别进行了很好的排序,但我不知道如何提交子类别,因此我可以按顺序输出它们。

我将如何在 Ember 中执行此操作,或者我应该在服务器端执行此操作并通过已排序的 API 传递数据?

4

1 回答 1

1
AC.IndexController = Ember.ArrayController.extend({
  sortProperties: ['order'],
  sortAscending: true,

  // Use an Ember.ObjectController for each Category
  itemController: 'category'
});

App.CategoryController = Ember.ObjectController.extend({
  init: function() {
    this._super();

    this.set('subcategoriesController', App.SubcategoriesController.create({
      category: this
    }));
  }
});

App.SubcategoriesController = Ember.ArrayController.extend({
  sortProperties: ['order'],
  sortAscending: true,

  content: function() {
    return this.get('category.subcats');
  }.property('category.subcats.[]')
});

然后您的索引模板应如下所示:

<ul>
  {{#each category in arrangedContent}}
    <li>
      {{category.name}}
      <ul>
        {{#each subcategory in category.subcategoriesController.arrangedContent}}
          <li>{{subcategory.name}}</li>
        {{/each}}
    </li>
  {{/each}}
</ul>
于 2013-07-24T13:25:38.813 回答