Reputation: 8202
This is the code sample from my application
Discussion Model
App.Discussion = Ember.Model.extend({
id: Ember.attr(),
title: Ember.attr(),
body: Ember.attr(),
course_id: Ember.attr(),
replies: Ember.hasMany('App.Reply', {key:'replies', embedded: 'load'}),
creator_id: Ember.attr(),
creator_first_name: Ember.attr(),
creator_last_name: Ember.attr(),
status: Ember.attr(),
created_at: Ember.attr(),
accepted_answer_id: Ember.attr()
});
Reply Model
App.Reply = Ember.Model.extend({
id: Ember.attr(),
body: Ember.attr(),
discussion_id: Ember.attr(),
discussion: Ember.belongsTo('App.Discussion', {key: 'discussion_id'}),
creator_id: Ember.attr(),
creator_first_name: Ember.attr(),
creator_last_name: Ember.attr(),
status: Ember.attr(),
created_at: Ember.attr()
});
App.Discussion.Controller = Ember.ObjectController.extend({
hasSomething: function() {
// Here I want to check whether user_id = 101 in the replies array of the discussion Model.
// If any of the replies has the user_id as 101 return true.
}.property('id')
});
I tried getting the replies into an array and then iterating and checking over it.
replies = this.get('model.replies');
replies.forEach(function(reply) {
console.log(reply.get('creator_id'));
if (window.userId === reply.get('creator_id')) {
// do stuff here.
}
});
Is there any better/EMBER WAY way to acheive this ???
Upvotes: 0
Views: 1524
Reputation: 17522
Ember has some nice helpers on their Array-object (also available on regular arrays unless you disable prototype extensions).
Array.isAny() might be worth looking at.
hasSomething: function() {
// Here I want to check whether user_id = 101 in the replies array of the discussion Model.
// If any of the replies has the user_id as 101 return true.
var replies = this.get('model.replies');
return replies.isAny('creator_id', window.userId);
}.property('model.replies.@each')
You might also want to set the property to actually observe the replies-array using model.replies.@each
.
Upvotes: 2