mercredi 5 octobre 2016

EmberJS computed.sort - sorting by associated model property

I have a simple belongsTo model relationship:

contract.js:

export default DS.Model.extend({
    object               : DS.belongsTo('object'),
    ....
})

object.js:

export default DS.Model.extend({
    street              : DS.attr('string'),
    zip                 : DS.attr('string'),
    city                : DS.attr('string'),
    ...
})

In my controller for an entity that holds many contracts, I'd like to sort by the street name of the associated object, but somehow this

export default Ember.Controller.extend({

    sortOrder: ['object.street'],
    sortedObjects: Ember.computed.sort('modelcontracts', 'sortOrder')

    ...
});

doesn't work.

Using a custom comparator function a la

function(a, b){
    if (a.street > b.street) {
      return 1;
    } else if (a.street < b.street) {
      return -1;
    }
}

I found out that a and b are Promises, but somehow I don't see how to get them sorted via a nested attribute (the object's street)




Aucun commentaire:

Enregistrer un commentaire