I am rather new to backbone, so its possible that i am violating the very essence of backbone in doing this. Suggestions are appreciated:
I have made a wall sort of system. So there is a form that can be used to post updates on the wall.
Each update can have comments on them. I am showing 10 updates at a time. So there are 10 comment forms. So I have a view:
CommentForm=Backbone.View.extend({
initialize:function(messageView){
},
events:{
"submit":"postcomment"
},
showMessage:function(data){
if(data.success)
type="success";
else
type="error";
message=data.error?data.error:"Update posted successfully";
$messageContainer=$this.prev();
console.log($this);
var html="<div class='alert alert-"+type+"'>"+message+"</div>";
$($messageContainer).html(html);
},
postcomment:function(){
$this=$(this.el);
$.post(baseUrl+"/portal/post-comment",$this.serialize(),this.showMessage,"json");
return false;
}
});
Now I create an instance to it as follows:
commentFormView= new CommentForm({el:$(".comment-form form")});
Note that .comment-form is a div. There are multiple such elements. The event handler gets attached to all the comment forms just fine. But when I use $this=$(this.el);
it always refers to the first comment form. How do I solve this. $(this.el) should refer to the current instance of comment form, where the event was triggered and not the first one