Ember - 如何使用新模型订购ArrayController

时间:2013-11-22 20:23:54

标签: ember.js

我有一个包含许多ObjectController(类别)的ArrayController(类别)。每个类别都有一个名为“category_order_nm”的属性。我已经设置了

App.CategoriesController = Ember.ArrayController.extend({
    itemController: 'category',
    sortAscending: true,
    sortProperties: ['category_order_nm']
});

到目前为止一切顺利!我的商店从我的API中获取所有类别,并按照他们应该的方式订购。现在,当我的用户点击“添加”时,它就变成了Edgy。我可以添加尽可能多的类别,他们会订购,但只能在新的类别中,就像它们是一个完全不同的数组。让我告诉你一个澄清的例子。我有这些首字母类别:

Category1 category_order_nm:10
Category2 category_order_nm:11
Category3 category_order_nm:12

我添加了这些类别

Category4 category_order_nm:3
Category5 category_order_nm:2
Category6 category_order_nm:1

他们将按以下顺序显示:

Category1 category_order_nm:10
Category2 category_order_nm:11
Category3 category_order_nm:12
Category6 category_order_nm:1
Category5 category_order_nm:2
Category4 category_order_nm:3

最后可能有用的信息。当用户点击我的路线CategoriesAddRoute时。发生这种情况:

App.CategoriesAddRoute = Ember.Route.extend({
    setupController: function(controller) {
        controller.newRecord();
    },
)};

App.CategoriesAddController = Ember.ObjectController.extend({   
    needs: ['categories'],
    newRecord: function() {
        record = this.store.createRecord( 'category', 
                                          {
                                              id: 4,
                                              category_active_bo: true,
                                              category_name_fr_sh: 'Nouvelle categorie',
                                              category_order_nm: 0,
                                          }
                                        );
        alert(this.get('controllers.categories').get('length')) // Given our previous example: 6
        this.get('controllers.categories').pushObject(record);
        alert(this.get('controllers.categories').get('length')) // Given our previous example: 7
        this.set('content', record);
    },
 )};

编辑:这是我的模板:

<script data-template-name="categories-template" type="text/x-handlebars">
        <li class="list-group-item" data-toggle="collapse" {{bindAttr data-target="category_collapse"}}>
            {{ category_name_fr_sh }}
            <button type="button" {{action destroyRecord this}}  class="btn btn-sm btn-danger pull-right">Effacer</button>
            <button type="button" class="btn btn-sm btn-success pull-right">Modifier</button>
        </li>  
    {{#if category_children_ids}}     
        <div {{bindAttr id="category_collapse_target"}} class="collapse">
            {{#each category_children_ids}}
                {{render "categories-template" this}}
            {{/each}}
        </div>
    {{/if}} 
</script>
<script type="text/x-handlebars" data-template-name="categories">
    <div class="col-md-3">
        {{#link-to 'categories.add' tagName="button" class="btn btn-sm btn-primary"}}Ajouter{{/link-to}}
        <ul class="list-group">                          
            {{#each category in controller}}
                {{{render "categories-template" this}}}
            {{/each}}
        </ul>
    </div>
    <div class="col-md-6">
        <div id="content">
            {{outlet}}
        </div>
    </div>
</script>

1 个答案:

答案 0 :(得分:0)

ArrayController扩展了Sortable mixin,您可以简单地执行以下操作:

songsController = Ember.ArrayController.create({   内容:歌曲,   sortProperties:['trackNumber'],   sortAscending:true });

http://emberjs.com/api/classes/Ember.SortableMixin.html