Search code examples
javascriptjquerycssbackbone.jsbackbone-events

Undelegate single event


I am having trouble stopping a click event on a dom element in a UL, essentially in the itemClicked method I want to undelegate the click event on the single element. When an item in a list is clicked, however that item still fires a click event despite have the none value. Is there a way to use undelegate for just the individual element clicked? I am at a loss as to why and any help is greatly appreciated.

App.Views.AttributeSelectorView = Backbone.View.extend({

  itemClicked: function (event) {
    var $target = $(event.target);
    this.trigger('itemClicked', $target.data('attr'), $target.text());

    this.undelegateEvents();
  }, 
  events: {
    'click [role=menuitem]': 'itemClicked'
  }
});   


 attributeSelectorView.on('itemClicked', function(attribute, display){
    // .remove attribute
    query.add({
      attribute: attribute,
      display: display
    }, {
      merge: true
    });
  });

Solution

  • try using stopListening, it Tell an object to stop listening to events.

    itemClicked: function (event) {
        var $target = $(event.target);
        this.trigger('itemClicked', $target.data('attr'), $target.text());
    
        this.stopListening();
      }