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('model.contracts', '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)
Edit
To clarify the code a little more:
contracts : Ember.computed.alias('model.contracts'),
street: Ember.computed.alias('realty.street'),
sortOrder: ['realty.street'],
sortedOwnedRealties: Ember.computed.sort('contracts.@each.street', function (a, b) {
console.log(Ember.get(a, 'id'));
console.log(Ember.get(a, 'street'));
//return 1;
})
That function prints undefined
to the console for the street
, but the correct id
.
I've renamed object
to realty
for clarity.