0

クイズ ビューがあり、質問のプロパティで並べ替えたいと考えていordます。クイズの唯一のプロパティが質問である場合、これは簡単ですが、次のようなモデル構造があります。

クイズモデル

App.Quiz = DS.Model.extend({
'badge': DS.belongsTo('App.Badge'),
'passingScore': DS.attr('number'),
'allowed': DS.attr('number'),
'questions': DS.hasMany('App.Question')
})

質問モデル

App.Question = DS.Model.extend({
'quiz': DS.belongsTo('App.Quiz'),
'text': DS.attr('string'),
'ord': DS.attr('number'),
'answers': DS.hasMany('App.Answer')
})

したがって、作成されるコントローラはオブジェクト コントローラであり、アレイ コントローラではありません。そのプロパティでソートする方法についてのアイデアはありますか?

4

1 に答える 1

2

わかりました、Ember には Ember.ArrayProxy と Ember.SortableMixin があるので、次のようなことができます。

var sortedElements = Ember.ArrayProxy.createWithMixins(Ember.SortableMixin, {
  content: yourElements.toArray(),
  sortProperties: ['propertyYouWantToSortBy'],
  sortAscending: false
});

他にも多くのオプションがあります。https://github.com/emberjs/ember.js/blob/master/packages/ember-runtime/lib/mixins/sortable.jsを参照してください。オーバーライドできる orderBy 関数:

orderBy: function(item1, item2) {
var result = 0,
    sortProperties = get(this, 'sortProperties'),
    sortAscending = get(this, 'sortAscending'),
    sortFunction = get(this, 'sortFunction');

Ember.assert("you need to define `sortProperties`", !!sortProperties);

forEach(sortProperties, function(propertyName) {
  if (result === 0) {
    result = sortFunction(get(item1, propertyName), get(item2, propertyName));
    if ((result !== 0) && !sortAscending) {
      result = (-1) * result;
    }
  }
});

return result;

}

アサートを削除して、sorProperties を設定せずに、次のように変更することもできます。

orderBy: function(item1, item2) {
var result = 0,
    sortAscending = get(this, 'sortAscending'),
    sortFunction = get(this, 'sortFunction');

// implement your sort logic here, I don't know how you want to sort it
result = sortFunction(get(item1, propertyName), get(item2, propertyName));


return result;

}

于 2013-08-23T17:02:47.050 に答える