2015-06-21 41 views
0

我已经看过几个例子,但还没有能够进行反向排序,因此新生成的对象处于顶部。通过ember上的时间戳对数据进行排序

我的可排序项目在组件中,我不认为我正确传递sortProperties & sortAscending。

lavender.js:

export default Ember.Controller.extend({ 
    needs: ['application'], 

    sortProperties: ['timestamp'], 
    sortAscending: false 
}); 

lavender.hbs

{{#each model.comment as |comment|}} 
    {{comment-thread message=comment.message user=comment.user timestamp=comment.timestamp sortProperties=sortProperties sortAscending=sortAscending}} 
{{/each}} 

comment.js

export default DS.Model.extend({ 
    message: DS.attr('string'), 
    timestamp: DS.attr('date'), 

    user: DS.belongsTo('user', {async: true}), 
    todo: DS.belongsTo('todo', {async: true}), 

}); 

todo.js(型号为lavender.js)

export default DS.Model.extend({ 
    title: DS.attr('string'), 
    isCompleted: DS.attr('boolean', {defaultValue: false}), 
    detail: DS.attr('string', {defaultValue: "add details.."}), 

    comment: DS.hasMany('comment', {async: true}), 
}); 

必须有我没有看到的东西..谢谢!

回答

1

如果您希望自己的方法工作或者您可以选择其他方法,则必须使用已弃用的Ember.ArrayController而不是Ember.Controller

最好的方法是使用Ember.computed宏:

export default Ember.Controller.extend({ 
    needs: ['application'], 

    commentsSorting: ['timestamp:desc'], 
    comments: Ember.computed.sort('model.comment', 'commentsSorting') 
}); 

然后,而不是model,迭代在模板上comments

您还可以使用计算财产和私有(灰心)Ember.ArrayProxy,就像这样:

export default Ember.Controller.extend({ 
    needs: ['application'], 

    comments: Ember.computed('model', 'model.comment', function() { 
     return Ember.ArrayProxy.createWithMixins(Ember.SortableMixin, { 
     sortProperties: ['timestamp'], 
     sortAscending: false, 
     content: this.get('model.comment') 
     }); 
    }) 
}); 

然后你就可以在你的模板遍历comments

{{#each model.comment as |comment|}} 
    {{comment-thread message=comment.message user=comment.user timestamp=comment.timestamp}} 
{{/each}} 

我不认为你需要将排序属性传递给comment-thread,我没有误解这是如何工作的。它在控制器中进行排序,所有记录都在这里,而不是在组件中,其中每1个组件只有1个记录,并且没有对其他记录的引用。

+0

谢谢!根据您的建议,我选择实施Ember.computed宏。我是新来的烬,所以我有一些基本的问题。 'timestamp:desc'代表什么? 我是否会替换Ember.computed.sort('comments')的Ember.computed.sort('model')? – sunoceansand

+0

另外,如何在模板中的ember.computed上重复评论? – sunoceansand

+0

1)'timetamp:desc'表示你想按'timestamp'属性降序排序。 2)我在那里放了'model',但是我看到你遍历'model.comment',所以'model.comment'应该在那里(如果这是一个模型数组,我不知道你没有给出代码模型)。 3)评论被排序'model.comment',所以你迭代iver'评论'在你的模板。 –

相关问题