最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Backbone.js attach view to multiple elements - Stack Overflow

programmeradmin0浏览0评论

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 ments on them. I am showing 10 updates at a time. So there are 10 ment forms. So I have a view:

    CommentForm=Backbone.View.extend({
initialize:function(messageView){

},
events:{
    "submit":"postment"
},
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);
},
postment:function(){
        $this=$(this.el);

        $.post(baseUrl+"/portal/post-ment",$this.serialize(),this.showMessage,"json");
        return false;
}


   });

Now I create an instance to it as follows:

  mentFormView= new CommentForm({el:$("ment-form form")});

Note that ment-form is a div. There are multiple such elements. The event handler gets attached to all the ment forms just fine. But when I use $this=$(this.el); it always refers to the first ment form. How do I solve this. $(this.el) should refer to the current instance of ment form, where the event was triggered and not the first one

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 ments on them. I am showing 10 updates at a time. So there are 10 ment forms. So I have a view:

    CommentForm=Backbone.View.extend({
initialize:function(messageView){

},
events:{
    "submit":"postment"
},
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);
},
postment:function(){
        $this=$(this.el);

        $.post(baseUrl+"/portal/post-ment",$this.serialize(),this.showMessage,"json");
        return false;
}


   });

Now I create an instance to it as follows:

  mentFormView= new CommentForm({el:$(".ment-form form")});

Note that .ment-form is a div. There are multiple such elements. The event handler gets attached to all the ment forms just fine. But when I use $this=$(this.el); it always refers to the first ment form. How do I solve this. $(this.el) should refer to the current instance of ment form, where the event was triggered and not the first one

Share Improve this question asked Oct 3, 2012 at 3:02 SoWhatSoWhat 5,6222 gold badges30 silver badges62 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

One way would be to create a new view for each element using something like this.

$(".ment-form form").each(function() {
    new CommentForm( { el: $(this) } );
});

Edit There is another (better?) way. Because the event handler gets the raw event as its first parameter, you can write the handler postment like this:

postment:function(evt){
   // ...
}

Then you can use $(evt.srcElement) to get the actual element.

postment:function(evt){
   $this = $(evt.srcElement);
   // ...
}

$('.ment-form form') will return an array of all the matching form elements. You need to iterate through that array and create a view for each element, like dbaseman showed.

Also, instead of doing

$this=$(this.el)

backbone views already provide a jquery wrapped el:

this.$el
发布评论

评论列表(0)

  1. 暂无评论