I have ember models called survey
, question
, and response
. survey
s have multiple question
s, which have multiple response
s. Each response
has an attribute count
.
How do I set a total_response_count
computed value in the survey
model? In emberjs 1.0.0, the questions
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.
I can easily access responses
from the question
model because responses
are embedded in question
. However, Ember automatically makes promises for the questions
referenced by survey
because {async: true}.
Survey Model:
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')
});
Question Model:
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')
});
Response Model:
App.Response = DS.Model.extend({
response: DS.attr('string'),
count: DS.attr('number'),
question: DS.belongsTo('question')
});
I'm using ember-1.0.0 and ember-data 1.0 beta-2.
this.get('questions')
PromiseArray? – Bradley Priestquestions
andquestions.isFulfilled
? – Bradley Priestquestions.@each
? – andrei1089