mercredi 21 janvier 2015

How to overwrite ember-data reader method? or convert DS.attr to computed property?

I have an endpoint that returns id, name, properties, and properties is a damn string, with comma separated attribute key/value pairs. Yes you read that correctly.


properties = "id:1,name:foobar,phone:123456789,taco:true"


Current Model



import DS from 'ember-data';

export default DS.Model.extend({
contact: DS.belongsTo("contact"),

name: DS.attr(),
phone: DS.attr(),
properties: DS.attr(),
eachProperties: function () {
return this.get('properties').split(",").map(function(propertyKeyValuePair) {
return propertyKeyValuePair.split(":");
});
}.property('properties')
});


properties requires some computation to be helpful on the front-end.


Can you re-use the property named properties instead of creating a new one called eachProperties?


Something like:



export default DS.Model.extend({
// ... other stuff

properties: DS.attr(function(model, obj){
return obj.split(",").map(function(propertyKeyValuePair) {
return propertyKeyValuePair.split(":");
});
});
});


OR how does one change the name of an api property name to something else like rawProperties and then have the computed property called properties?


I know you're thinking "Just fix your damn API" but you know that isn't always possible :)





Aucun commentaire:

Enregistrer un commentaire