Using ember data, I've run across an issue during serialization where computed properties are not included in the payload.
var Person = DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
fullName: function( ) {
return this.firstName + this.lastName;
}.property()
});
App.store.createRecord( Person, {
firstName: 'John',
lastName: 'Doe'
});
App.store.commit();
Results in the following payload:
{ firstName: "John",
lastName: "Doe" }
I've tried adding .cacheable()
to the property, but it didn't seem to help. I've also tried wrapping the entire fullName
function in Ember.computed()
, but that didn't seem to help, either.
Tracing the Ember code, I see that the data for the request comes from DS.Model.serialize()
which collects all the attributes for the model. However, it does not seem to collect computed properties.
Ember Code Snippet:
serialize: function(record, options) {
options = options || {};
var serialized = this.createSerializedForm(), id;
if (options.includeId) {
if (id = get(record, 'id')) {
this._addId(serialized, record.constructor, id);
}
}
this.addAttributes(serialized, record);
this.addRelationships(serialized, record);
return serialized;
},
addAttributes: function(data, record) {
record.eachAttribute(function(name, attribute) {
this._addAttribute(data, record, name, attribute.type);
}, this);
}
As you can see, they collect attributes and relationships, but there doesn't seem to be anything collecting computed properties. My strategy at first was to overload addAttributes()
to also loop through all computed properties and add them to the list. But in my attempt, couldn't figure out a reliable way to get a list of computed properties. If I made the properties cacheable, I could use Ember.meta( model, 'cache' )
but that list includes all attributes, computed properties, and a few extras that I don't need/want.
So, my questions after all of this...
Is there a way in Ember that already exists to cause computed properties to be included in the serialization?
If not, I can overload appropriate methods, but how do I get a dynamic list of all computed properties? (I can use
.getProperties()
but it expects an array of property names, which I don't have)Any other relevant suggestions?