Kamil Davudov
Kamil Davudov

Reputation: 363

How turn off all events before new view rendering

I have several views. In some of them I have the similar events like

events: {
    'click #save': 'save'
}

When I create and render new view old event listening remains so old algorythm still works when I already change the view.

As I know there is a stopListening() function but how can I activate for all previous views. So when I change the view/page I want disable all previous events. How I can do that?

Upvotes: 1

Views: 55

Answers (2)

mador
mador

Reputation: 1223

try to use el.

var MyView = Backbone.View.extend({
    el: '#wrapper_of_save_element',

    events: {
       'click #save': 'save'
    },

    save: function() {
        ...
    }
});

so your event is only inside your #wrapper_of_save_element (eg. a wrapper div)

http://backbonejs.org/#View-el

Upvotes: 0

Ignacio A. Rivas
Ignacio A. Rivas

Reputation: 636

ID's are global, you shouldn't have more than one per page. Append your events to a class instead.

events: {
'click .save-btn': 'save'
}

Also, make sure you're disposing your views once you finished using them:

var MyView = Backbone.View.extend({
  events: {
   'click .save-btn': 'save'
  },

  ...

  dispose: function() {
    this.unbind();
    this.remove();
  }
};

var view = MyView();
...
view.dispose();

Cheers.

Upvotes: 2

Related Questions