Basically what I need is to do something like this
App.CommentView = Backbone.View.extend({
className: function() {
if (this.model.get('parent_id')) {
return 'comment comment-reply';
} else {
return 'comment';
}
},
The problem is, that at the function passed to className
is executed in context of the html of the view template, so I can't call this.model
.
Is there any way I can access the model at this point in the rendering process? Or do I need to set the class later, for example in the render
function?
This sounds like a job for model binding.
It would be much easier I think to use
this.$el.toggleClass
or simply add the class insiderender
.However if you want to set the class when constructing the view, you can pass it as an option:
I did it at View initialize
You should use the attributes hash/function: