Computed Ember data property for async relationship

I have some models configured with asynchronous relationships, for example:

User = DS.Model({ postsApproved: DS.hasMany('post', {async: true, inverse: 'approved'}) }) Post = DS.Model({ approver: DS.belongsTo('user', {async: true, inverse: 'postsApproved'}) }) 

In the controller, I have a property in the message, isApproved , which simply checks if approver is approver . I expect this to work:

 isApproved: function() { return !Ember.isNone(this.get('approver')); } 

But this always returns true, and if I check the data, I see that this is because this.get('approver') returns a promise. It works:

 isApproved: function() { return !Ember.isNone(this.get('approver.content')); } 

But using content seems to me too messing around with internal elements. Is this the right way to do this, or am I missing something?

+5
source share
1 answer

You are correct, testing for this.get('approver.content') is incorrect, since the element may have a related approver , but it has not yet been loaded.

Try loading the associated model on the route:

 // route.js model: function(params) { return this.store.find('post', params.post_id).then(function(post) { return Ember.RSVP.hash({ post: post, approver: post.get('approver') }); }); }, setupController: function(controller, model) { controller.set('model', model.post); controller.set('approver', model.approver); }, // controller.js isApproved: function() { return !Ember.isNone(this.get('approver')); }.property('approver') 
0
source

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


All Articles