How to get a list of all calculated properties?

Using ember data, during serialization, I ran into a problem in which 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 tried adding the .cacheable() property to the property, but it didn't seem to help. I also tried to wrap the entire fullName function in Ember.computed() , but that didn't help either.

Tracking 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 the calculated 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 it seems that they do not collect computable properties. My first strategy was to overload addAttributes() to also iterate over all computed properties and add them to the list. But in my attempt, I could not find a reliable way to get a list of computed properties. If I made the properties cacheable, I could use Ember.meta( model, 'cache' ) , but this list includes all the attributes, calculated properties, and a few additional functions that I don't need / need.

So my questions after all this ...

  • Is there a way in Ember that already exists to include computed properties in serialization?

  • If not, I can overload the corresponding methods, but how do I get a dynamic list of all calculated properties? (I can use .getProperties() , but it expects an array of property names that I don't have)

  • Any other relevant suggestions?

+4
source share
1 answer

I have not tried, but does eachComputedProperty fit your goals?

+1
source

All Articles