2015-06-21 86 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'評論'在你的模板。 –

相關問題