Backbone.js events in my views fire several times

I may have misunderstood how to implement backbone.js, because all my views that support multiple models (for example, the Product view, which can display multiple products) send events to all the views that were created in this session.

So, in the example below, when I click the #redirect_product link, "redirect_product" is called several times depending on the number of products that I have seen. If I looked at 5 products, on the 6th click I will receive 6 warnings.

What's going on here?

  505     /****************PRODUCT VIEW****************/
  506     App.Views.Product = Backbone.View.extend({
  507         el: $('#content_sec'),
  508 
  509         events: {
  510             "click #redirect_product": "redirect_product",
  511         },
  512 
  513         initialize: function(options) {
  514             this.model = this.options.model;
  515             this.render();
  516 
  517         },
  518 
  519         render: function() {
  520             $(this.el).empty();
  521             $('#product_detail_template').tmpl(this.model.toJSON()).appendTo($(this.el));
  522 
  523 
  524             //Activate facebook buttons
  525             if (typeof FB  != "undefined"){
  526                         FB.XFBML.parse(document.getElementById('item_share'))
  527             }
  528 
  529             wishlist.init();
  530             return this;
  531         },
  532 
  533         redirect_product: function() {
  534             //Send data on what product being clicked by whom
  535             alert('hi');
  536 
  537 
  538             //Open new window with product for user
  539             var external_link = this.model.get('product').attributes['External Link'];
  540             window.open(external_link, "external_site");
  541 
  542         },
  543     });
+5
source share
3 answers

, , , el .

, - :

$('#content_sec').append('<div class="productView"></div>');
var product = new Product();
var view = new ProductView({model: product, el: $('.productView:last')});

, , .

+8

, :

  • ( ) - <ul>
  • ( ) - <li>

.

li, ( ) .

, .

+3

id "#redirect_product" ".redirect_product". .

. id. ,

this.$(".redirect_product")

, .

+1

All Articles