Binding events to views - Firing uneven number of times

后端 未结 2 662
挽巷
挽巷 2021-01-29 06:47

I have UserModel: UserView and UserCollection: UserCollectionView. With this, I am trying to bind a click event

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-29 07:15

    The main problem is that you are rendering all UserView views in the same container:

     $('#users-list').append(this.template(this.model.toJSON()));
    

    This is why the click event selector('click .user-data') is selecting the other UserView buttons and firing it's clicks too.

    You didn't ask but... here it goes some suggestions:

    1. Do not call render from the initialize function;
    2. Avoid defining the el;
    3. Avoid adding the view content outside the view using $. Use this.$el instead;
    4. NEVER forget to return this; at you render functions.

    Here is a jsfiddle with what I think you want: https://jsfiddle.net/Neviton/n52j873u/

提交回复
热议问题