I can't seem to get 'computed' properties from a model. In this case, I cannot get the property commented_by
My app structure is pretty simple: I have a Post model that has many comments. In the post.hbs where I display a post, I have a section that renders all comments associated with this post, like so:
{{#each comment in comments}}
{{render "comment" comment}}
{{/each}}
My comment model (in Rails) is as follows:
class Comment < ActiveRecord::Base
extend ActsAsTree::TreeView
belongs_to :post
belongs_to :user
def commented_by
self.user.username
end
end
And it's subsequent serializer is as follows:
class CommentSerializer < ActiveModel::Serializer
embed :ids, include: true
attributes :id,
:parent_id,
:body,
:created_at,
:updated_at,
:post_id,
:user_id,
:commented_by
belongs_to :post
belongs_to :comment
has_many :children, include: true, root: :comments
end
Therefore I intend to serialize the commented_by 'computed property' and send it along. Now, on the client side of things:
My ember comment model is as follows:
App.Comment = DS.Model.extend({
parentID: DS.attr('number'),
body: DS.attr('string'),
createdAt: DS.attr('date'),
updatedAt: DS.attr('date'),
postID: DS.attr('number'),
userID: DS.attr('number'),
commentedBy: DS.attr('string'),
post: DS.belongsTo('post', {async: true}),
});
I guess the route, controller and view may be relevant as well:
App.CommentRoute = Ember.Route.extend({
model: function(params){
return this.store.find('comment', params.id);
},
})
App.CommentView = Ember.View.extend({
templateName: 'comments/comment',
});
App.CommentController = Ember.ObjectController.extend({
testProperty: "hello"
})
My questions therefore are these: 1. (main question:) Why is it that the commented_by property isn't being populated on the client side? (server side console wise it shows just fine.)
Follow up questions: 2. I have noticed that the last server call I got was GET /posts/2. Does this mean that Ember's render doesn't make a call to get the post's comments? I still have the comments showing, though, just not the commented_by property (all other properties are in the database, in columns.), so I can only guess that Ember is not going through the show action in comment_controller.rb; if so, how do I serialize the data properly to include commented_by?
- When I put a debugger in the commentview's didinsertelement and try and fetch Comment's controller's 'testProperty', i get a null value. Does this mean that render does not show any Ember-Controller properties?
I know it's... a loaded question, but I find that I am unclear on these issues and I'm hoping someone here can help me out.