Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get a list of all computed properties?
    primarykey
    data
    text
    <p>Using ember data, I've run across an issue during serialization where computed properties are not included in the payload.</p> <pre><code>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(); </code></pre> <p>Results in the following payload:</p> <pre><code>{ firstName: "John", lastName: "Doe" } </code></pre> <p>I've tried adding <code>.cacheable()</code> to the property, but it didn't seem to help. I've also tried wrapping the entire <code>fullName</code> function in <code>Ember.computed()</code>, but that didn't seem to help, either.</p> <p>Tracing the Ember code, I see that the data for the request comes from <code>DS.Model.serialize()</code> which collects all the attributes for the model. However, it does not seem to collect computed properties.</p> <p>Ember Code Snippet:</p> <pre><code>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); } </code></pre> <p>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 <code>addAttributes()</code> 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 <code>Ember.meta( model, 'cache' )</code> but that list includes all attributes, computed properties, and a few extras that I don't need/want.</p> <p>So, my questions after all of this...</p> <ul> <li><p>Is there a way in Ember that already exists to cause computed properties to be included in the serialization?</p></li> <li><p>If not, I can overload appropriate methods, but how do I get a dynamic list of all computed properties? (I can use <code>.getProperties()</code> but it expects an array of property names, which I don't have)</p></li> <li><p>Any other relevant suggestions?</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload