Reputation: 957
I have an model, userhashtag
, that belongs to hashtag
(ie one-many). As its name suggests, the userhashtag
model links the user
model with the hashtag
model.
The userhashtag
model only contains the user_id and the hashtag_id. I have an ember template that lists all of the userhashtags for a certain user_id, and I want to display the userhashtags with the name attribute, contained in its parent, the hashtag model.
Here is my userhashtag model:
App.Userhashtag = DS.Model.extend({
userId: DS.attr('number'),
hashtagId: DS.attr('number'),
createdAt: DS.attr('date'),
updatedAt: DS.attr('date'),
user: DS.belongsTo('user'),
hashtag: DS.belongsTo('hashtag'),
hashtagname: function(){
var h = this.store.find('hashtag', this.get('hashtagId'));
return h.get('name');
}.property('hashtagId')
});
And I'm trying to call it in my template like this:
{{userhashtag.hashtagname}}
I'm using the ember extension for chrome developer tools, and I can see that it is correctly loading all the hashtags
and userhashtags
. However, I still don't see the name attribute. Upon inspection, the store is correctly finding the corresponding hashtag, but the var h
is returning as undefined.
Would anyone have any suggestions on why the hashtagname
function isn't returning the name? Also, I feel like I'm not doing this in best practice, what should I be doing instead?
Thanks!
Upvotes: 0
Views: 90
Reputation: 1
You should remove hastagId
and userId from your model. When ember-data tries to parse the model and finds a somemodel_id
and also a belongsTo
with the same name (without _id
) it then also loads the belongsTo
(or it loads, when some properties of it are needed, if I remember correct).
Upvotes: 0
Reputation: 47367
Firstly, h should be a promise, not a record.
Secondly, does hashTag belongsTo work?
hashtagname: function(){
return this.get('hashtag.name');
}.property('hashtag')
At the same time in the template you could just as easily do
{{hashtag.name}}
You shouldn't be using the store inside of a model, I'm not even positive how you did that without injecting the store into everything. (I'm intrigued for kicks and giggles you should post it)
Upvotes: 1