Ric*_*oss 1 javascript ember.js ember-data
我有一个项目模型,每个项目可以有很多项目.这可以是很多层次.
我有这个jsfiddle http://jsfiddle.net/rmossuk/xmcBf/3/
如何更改此项以便能够拥有多个级别的项目,以及如何循环显示每个级别以显示它们?
我还需要能够订购每个项目子项并将该命令存储到服务器.
请有人能告诉我你怎么做到这一点?
更新:
我现在已经设法实现了这个问题,可以在很多层面深入探讨这个问题,请看http://jsfiddle.net/rmossuk/fBmmS/3/
但我需要一种方式来说明每个项目的顺序,并按顺序显示它们.目前它只显示itemIds数组中的子项,但这只用于关联目的,我不能改变它重新排序可以吗?
有人知道怎么做吗 ?
非常感谢瑞克
Mik*_*ski 13
在视图中,使用sortedItems计算属性,定义如下:
JS
App.Item = DS.Model.extend({
name: DS.attr('string'),
index: DS.attr('number'),
items: DS.hasMany('App.Item', {key: 'itemIds'} ),
item: DS.belongsTo('App.Item'),
product: DS.belongsTo('App.Product'),
sortedItems: function () {
var items = this.get('items').toArray();
return items.sort(function (lhs, rhs) {
return lhs.get('index') - rhs.get('index');
});
}.property('items.@each.isLoaded')
});
Run Code Online (Sandbox Code Playgroud)
在这里查看完整的解决方案:http://jsfiddle.net/MikeAski/K286Q/3/
编辑
根据您的要求,这是另一种解决方案,将排序的ID保持在父级内(以最小化更新和索引管理):http://jsfiddle.net/MikeAski/K286Q/12/
JS
App.Item = DS.Model.extend({
name: DS.attr('string'),
items: DS.hasMany('App.Item', {key: 'itemIds'} ),
sortedIds: DS.attr('string', { key: 'sortedIds' }),
item: DS.belongsTo('App.Item'),
product: DS.belongsTo('App.Product'),
sortedChildren: function () {
if (!this.get('isLoaded')) {
return [];
}
var sortedIds = this.get('sortedIds').split(','),
items = this.get('items').toArray();
return sortedIds.map(function (id) {
if (id === '') {
return null;
}
return items.find(function (item) {
return item.get('id') == id;
});
}).filter(function(item) {
return !!item;
});
}.property('isLoaded', 'sortedIds', 'items.@each.isLoaded')
});
Run Code Online (Sandbox Code Playgroud)
稍微复杂一点......