Note that there are some explanatory texts on larger screens.

plurals
  1. POEmber-data model computed value, return value instead of promise
    primarykey
    data
    text
    <p>I have ember models called <code>survey</code>, <code>question</code>, and <code>response</code>. <code>survey</code>s have multiple <code>question</code>s, which have multiple <code>response</code>s. Each <code>response</code> has an attribute <code>count</code>.</p> <p>How do I set a <code>total_response_count</code> computed value in the <code>survey</code> model? In emberjs 1.0.0, the <code>questions</code> are in a DS.PromiseArray (due to the async: true), so when I return the computed value, it shows up in my template as an Object rather than a value.</p> <p>I can easily access <code>responses</code> from the <code>question</code> model because <code>responses</code> are embedded in <code>question</code>. However, Ember automatically makes promises for the <code>questions</code> referenced by <code>survey</code> because {async: true}.</p> <p>Survey Model:</p> <pre><code>App.Survey = DS.Model.extend({ title: DS.attr('string'), owner_id: DS.belongsTo('user'), questions: DS.hasMany('question', {async:true}), total_responses: function() { var question_cb = function(prevValue, item) { return prevValue + item.get('total_responses'); }; return this.get('questions').then(function(questions){ return questions.reduce(question_cb, 0); }); }.property('questions') }); </code></pre> <p>Question Model:</p> <pre><code>App.Question = DS.Model.extend({ survey: DS.belongsTo('survey'), question: DS.attr('string'), responses: DS.hasMany('response'), total_responses: function() { var response_cb = function(prevValue, item) { return prevValue + item.get('count'); }; return this.get('responses').reduce(response_cb, 0); }.property('responses') }); </code></pre> <p>Response Model:</p> <pre><code>App.Response = DS.Model.extend({ response: DS.attr('string'), count: DS.attr('number'), question: DS.belongsTo('question') }); </code></pre> <p>I'm using ember-1.0.0 and ember-data 1.0 beta-2.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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