framework7

Open full view…

Events are triggered multiple times

dingle
Sun, 13 Apr 2014 13:13:19 GMT

Hi, I think I found a bug. I've got a piece of code like this: --- javascript $$(document).on('pageInit', function (e) { var page = e.detail.page; if (page.name === 'page'){ $$(document).on("change", "[type='checkbox']", function(){ console.log(this.id + " " + this.checked); // event action goes here } } } --- The events in this trigger are triggered once for each time that I visited that page in the session. If I visited that page 4x in the session the events are triggered 4x. It seems a new copy of that page is created every time I load it instead of replacing the old page. It might have something to do with the checkboxes or the way I listen to their changes because I don't have the problem with a range input on another page.

dingle
Sun, 13 Apr 2014 13:13:19 GMT

Hi, I think I found a bug. I've got a piece of code like this: --- javascript $$(document).on('pageInit', function (e) { var page = e.detail.page; if (page.name === 'page'){ $$(document).on("change", "[type='checkbox']", function(){ console.log(this.id + " " + this.checked); // event action goes here } } } --- The events in this trigger are triggered once for each time that I visited that page in the session. If I visited that page 4x in the session the events are triggered 4x. It seems a new copy of that page is created every time I load it instead of replacing the old page. It might have something to do with the checkboxes or the way I listen to their changes because I don't have the problem with a range input on another page.

idangerous
Sun, 13 Apr 2014 15:22:38 GMT

Everything is correct, because with this syntax: --- $$(document).on("change", "[type='checkbox']", function(){ .... }) --- you adds live event listener every time the same page was loaded. If you need to handle events for checkboxes on specific page it is better to use usual events: --- if (page.name === 'page'){ $$(page.container).find("[type='checkbox']").on("change", function(){ console.log(this.id + " " + this.checked); // event action goes here } }

dingle
Mon, 14 Apr 2014 15:35:36 GMT

Thanks that makes sense. I only recently got back to Javascript after ten years so I'm learning again :)