The calculated value of the Ember data model, the return value instead of the promise

I have ember models called survey, questionand response. surveyhave multiple questions, which have multiple responses. Everyone responsehas an attribute count.

How to set the calculated value total_response_countin the model survey? In emberjs 1.0.0, they questionsare in DS.PromiseArray (due to async: true), so when I return the computed value, it appears in my template as an object, not a value.

I can easily access responsesfrom the model questionbecause it responsesis built in question. However, Ember automatically creates promises for the questionsreferenced surveybecause {async: true}.

Poll 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')
});

Answer Model:

App.Response = DS.Model.extend({
  response: DS.attr('string'),
  count: DS.attr('number'),

  question: DS.belongsTo('question')
});

I am using ember-1.0.0 and ember-data 1.0 beta-2.

+4
source share
1 answer

I also asked this question on Github and received this answer from Yehuda Katz:

You can try something like this:

App.Survey = DS.Model.extend({
  title: DS.attr(),
  owner: DS.belongsTo('user'),
  questions: DS.hasMany({ async:true }),

  totalResponses: Ember.arrayComputed('questions', {
    initialValue: 0,
    addedItem: function(accum, item) {
      accum += item.get('totalResponses');
    },
    removedItem: function(accum, item) {
      accum -= item.get('totalResponses');
    }
  })
});

When issues are resolved, the addItem callback in totalResponses will be called once for each element in the allowed array.

+6
source

Source: https://habr.com/ru/post/1502479/


All Articles