jeudi 19 février 2015

How to define a computed property for async associations in Ember data?

I am having trouble defining a computed property, based on some async associations, in the object controller. I based my example on some examples I found here and in the Ember docs.


I have three (relevant) models: A Space with one or more Subscription(s), which each have a User. They are all loaded async:



MyApp.Space = DS.Model.extend({
name: DS.attr('string'),
subscriptions: DS.hasMany('subscription', { inverse: 'space', async: true })
});

MyApp.Subscription = DS.Model.extend({
space: DS.belongsTo('space', { inverse: 'subscriptions', async: true }),
user: DS.belongsTo('user', { async: true })
});

MyApp.User = DS.Model.extend({
name: DS.attr('string')
});


I try to compute a property mySubscription, which gets the subscription belonging to me from subscriptions, in the controller for the space because that is where I have access to the current user through a mixin(not particularly relevant for this example).



MyApp.SpaceController = Ember.ObjectController.extend(
MyApp.CurrentUserMixin, {

mySubscription: function () {
var me = this.get('currentUser');
var subscriptions = this.get('model.subscriptions');
return subscriptions.findBy('user', me);
}.property('model.subscriptions.@each')
});


This property always turns out to be undefined however I try it. I've tried adding .content to everything async, I tried finding by id and debugged and inspected the hell out of it. Somehow, I am not able to find anything within the array of associated objects. Anybody got an idea how I should proceed?





Aucun commentaire:

Enregistrer un commentaire