我有两个对象如下:
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按顺序输出所有类别(通过''order'属性)。所以代码看起来像这样:
AC.IndexRoute = Ember.Route.extend({
model: function() {
return AC.Category.find();
}
});
AC.IndexController = Ember.ArrayController.extend({
sortProperties: ['order'],
sortAscending: true
});
这对顶级类别排序很好,但我无法弄清楚如何提交子类别,以便我可以按顺序输出。
我如何在Ember中执行此操作,或者我应该在服务器端执行此操作并通过已排序的API传递数据?
答案 0 :(得分: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>