pseudoanime
pseudoanime

Reputation: 1593

Sort ember data

I have a model named variableDataPackages whose structure is as follows:

TM.VariableDataPackage = DS.Model.extend({ 
    name: DS.attr(),
    number: DS.attr(),
    variableData: DS.attr(),  
    vds:DS.attr(), 
    asset_id:DS.attr(),
    vdp_id:DS.attr(), 
    type: DS.attr(),
    description: DS.attr(), 
    vdpackage_id:function(){
        if((this.get("type.name") != "d")&&(this.get("type.name") != "u")){            
            return this.get("type.name")  
        } else {
            return this.get("type.name") + this.get("number");                
        }
    }.property("type","number")   
})

The display value is a computed property and the set of records contained within the store for this model is frequently updated/deleted/created.

I want to be able to sort the records based on the vdpackage_id.

Is there any way to do this?

Upvotes: 0

Views: 895

Answers (1)

jnfingerle
jnfingerle

Reputation: 713

You could use the Ember.SortableMixin on the controller (examples can be found in the guides). This would enable you to sort for diffent keys and to make this dependent on user input.

Anywhere in your code you can create an Ember.ArrayProxy. An example for a model hook (untested, just to show the idea) would be:

 model: function () {
  return Ember.ArrayProxy.extend({
            arrangedContent: Ember.computed.sort('content', 'props'),
            props: ['bar:asc']
         }).create({
            content: this.store.all('foo')});
  });
}

Upvotes: 1

Related Questions