lundi 26 septembre 2016

Ember.computed.sort property not updating

I've been cracking my head for the last several days, trying to understand what am I doing wrong. I'm implementing an infrastructure of lists for my app, which can include paging/infinite scroll/filtering/grouping/etc. The implementation is based on extending controllers (not array controllers, I want to be Ember 2.0 safe), with a 'content' array property that holds the data.

I'm using Ember.computed.sort for the sorting, and it's working, but i have a strange behaviour when i try to change the sorter. the 'sortedContent' is not updating within the 'displayContent', even though the 'sortingDefinitions' definitions are updated.

This causes a weird behaviour that it will only sort if I "sort" it twice, as if the sorting was asynchronous.

I am using Ember 1.5 (but it also happens on 1.8) (attaching a snippet of code explaining my problem)

sortingDefinitions: function(){
    var sortBy = this.get('sortBy');
    var sortOrder = this.get('sortOrder') || 'asc';

    if (_.isArray(sortBy)) {
        return sortBy;
    }
    else {
        return (sortBy ? [sortBy + ':' + sortOrder] : []);
    }
}.property('sortBy', 'sortOrder'),

sortedContent: Ember.computed.sort('content', 'sortingDefinitions'),




displayContent: function() {
    var that = this;
    var sortBy = this.get('sortBy');
    var sortOrder = this.get('sortOrder');
    var list = (sortBy ? this.get('sortedContent') : this.get('content'));

    var itemsPerPage = this.get('itemsPerPage');
    var currentPage = this.get('currentPage');
    var listItemModel = this.get('listItemModel');

    return list.filter(function(item, index, enumerable){
        return ((index >= (currentPage * itemsPerPage)) && (index < ((currentPage + 1)  * itemsPerPage)));
    }).map(function(item) {
        var listItemModel = that.get('listItemModel');
        if (listItemModel) {
            return listItemModel.create(item);
        }
        else {
            return item;
        }
    });

}.property('content.length', 'sortBy', 'sortOrder', 'currentPage', 'itemsPerPage')

Thanks in advanced, Manu




Aucun commentaire:

Enregistrer un commentaire