簡體   English   中英

Ember.js訂單有很多孩子

[英]Ember.js order hasMany children

我有一個項目模型,每個項目可以有很多項目。 這可能有很多層次。

我有這個jsfiddle http://jsfiddle.net/rmossuk/xmcBf/3/

如何更改此選項以使其具有多個級別的項目,以及如何循環顯示每個級別的項目?

我還需要能夠訂購每個子項,並將其存儲到服務器。

請任何人告訴我如何做到這一點?

更新:

我現在設法實現了項目,可以從多個層面深入探討此問題,請參見此處http://jsfiddle.net/rmossuk/fBmmS/3/

但是我需要一種方法來說明每個子項的順序,並以該順序顯示它們。 目前,它僅顯示itemIds數組中的子項,但這僅用於關聯目的,我無法更改此順序以重新排序?

有人知道怎么做嗎 ?

非常感謝里克

在視圖中,使用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')
});

在此處查看完整的工作解決方案: 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')
});

盡管如此,還是有點復雜...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM