【问题标题】:Is there an informative article about how to use the SortableMixin?是否有关于如何使用 SortableMixin 的信息性文章?
【发布时间】:2012-08-24 15:56:37
【问题描述】:

目前我正在使用 JavaScript 的原生 sort() 方法在 Ember 中进行排序,但实际上我知道我应该使用 Ember 自己的排序,但不幸的是我完全不知道如何使用它。

我不想浪费人们的时间来为我解释它(除非你是一个受虐狂),但如果有人知道一篇关于如何在 EmberJS 中对模型集合进行排序的精彩文章,那么它会更多比赞赏!

目前我有类似以下的内容,我只是再次使用本机排序实现排序,但我停在那里,因为我想学习正确的方法。

SortOption: Ember.View.extend(
{
    template:   Ember.Handlebars.compile('<li><a href="javascript:void(0);">{{ option }}</a></li>'),
    option:     null,

    click: function()
    {
        var rootView    = this.nearestWithProperty('people');
        var sortBy      = this.get('option');

        var sorted = rootView.get('people').orderBy('formalName');
        rootView.set('people', sorted)
    }
})

【问题讨论】:

标签: ember.js


【解决方案1】:

This pull request 用于更新有关 SortableMixin 的文档。

Ember.SortableMixin provides a standard interface for array proxies
to specify a sort order and maintain this sorting when objects are added,
removed, or updated without changing the implicit order of their underlying
content array:
  songs = [ 
   {trackNumber: 4, title: 'Ob-La-Di, Ob-La-Da'},
   {trackNumber: 2, title: 'Back in the U.S.S.R.'},
   {trackNumber: 3, title: 'Glass Onion'},
  ];  

  songsController = Ember.ArrayController.create({
  content: songs,
  sortProperties: ['trackNumber']

 });

 songsController.get('firstObject'); // {trackNumber: 2, title: 'Back in the U.S.S.R.'}
 songsController.addObject({trackNumber: 1, title: 'Dear Prudence'});
 songsController.get('firstObject'); // {trackNumber: 1, title: 'Dear Prudence'}

Sortable Mixin 现在包含在 ArrayController 中,从 0.9.8 版本开始(我相信)。您需要做的就是在您的控制器上定义一个 sortProperties 数组并在您的视图中使用 arrangedContent 属性。 Here is an explanation from Ivan.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多