I am trying to make my very first search app.
After the app is built, every DOM is rendering as I expect and events work as well. When I dig deeper into it, I find a strange behavior, and after I did some search, I found it is because of zombie view events delegate issue.
Here is some part of my code:
var searchList = Backbone.View.extend({
events:{
'click #submit':function() {
this.render()
}
},
render() {
this.showList = new ShowList({el:$('.ADoM')});
}
});
When #submit
is clicked, a new instance of ShowList
will be created and '.ADoM'
DOM will be rendering.
showList.js
var showList = Backbone.View.extend({
events: {
"click .testing": function(e) {
console.log(e.currentTarget);
},
},
initialize() {
this.$el.html(SearchListTemplate());
}
});
The '.testing'
button event is bound with it.
So as what 'zombie' does, after multiple clicks on submit, then clicking the '.testing'
button, console.log()
will output multiple time.
I have followed the article here and tried to understand and fix my issue, and also tried to add this.remove()
in showList.js
as someone mentioned, but unfortunately it may because I was not able to place them in the proper place in my code, the issue is still unsolved.