Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Computed value in DS.Model returns Promise instead of Value #1315

Closed
azhang opened this issue Sep 16, 2013 · 2 comments
Closed

Computed value in DS.Model returns Promise instead of Value #1315

azhang opened this issue Sep 16, 2013 · 2 comments

Comments

@azhang
Copy link

azhang commented Sep 16, 2013

more specific details here: http://stackoverflow.com/questions/18835533/ember-data-model-computed-value-return-value-instead-of-promise

I'm trying to show a computed value in my template, but since the computed value uses an async linked model, it returns a promise. Is there any way to get or set the value when the promise is fulfilled?

@wycats
Copy link
Member

wycats commented Sep 18, 2013

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 questions resolves, the addedItem callback in totalResponses will be called once for every item in the resolved array.

@wycats wycats closed this as completed Sep 18, 2013
@azhang
Copy link
Author

azhang commented Sep 18, 2013

thanks! I'll try that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants