framework7

Open full view…

on() and off() for handling events

seme1
Tue, 24 Jun 2014 15:10:32 GMT

I am using the following to catch all "click" events for old and newly adding links in the page --- $$('.container').on('click', 'a.item-link', function(e){ // do some stuff }); --- The problem is when attempting to use the following code to remove the click events (or disable the links) when viewing a different page, I get an error message and it doesn't work !! --- $$('.container').off('click', 'a.item-link') --- With JQuery, the above works just fine. Any hints on why it is not working with F7 ?

Vladimir Kharlampidi
Tue, 24 Jun 2014 16:23:51 GMT

As you should know there is no way detach event that was added with anonymous function (like in your example). It works in jQuery because they use so called proxy funcs which is not best approach in my opinion. So you should use it like with native JS rules: --- function handleClick(e){ //do some stuff } $$('.container').on('click', 'a.item-link', handleClick); //to detach event $$('.container').off('click', 'a.item-link', handleClick);

seme1
Wed, 23 Jul 2014 22:05:05 GMT

The following registers fine: --- $$("#upPicsHolder").on('click', 'a.newImgDeleteBtn', deleteCurImg); --- However, I am having trouble with the following to de-register the click event --- $$("#upPicsHolder").off('click', 'a.newImgDeleteBtn', deleteCurImg); --- with Firebug, the following error message is displayed in the console when the second "off" statement is executed --- TypeError: Argument 2 of EventTarget.removeEventListener is not an object. Dom7.prototype.off() framework7.js (line 4666) this[j].removeEventListener(events[i], listener, false);

seme1
Thu, 24 Jul 2014 09:35:09 GMT

After further testing, it seems that F7 does not like this format for some reason --- $$(documentElement).off('click', 'a', functionName); --- The above seems to show the error message displayed above. The "on" function seems to register just fine. It's only the "off" function that does not work

Vladimir Kharlampidi
Fri, 25 Jul 2014 17:20:44 GMT

Hi there, No, live events don't work like that, and at the moment there is no way to remove live event listener :( Just use usual events instead of live events here

Vladimir Kharlampidi
Sat, 26 Jul 2014 12:29:18 GMT

This will be fixed in next update, already fixed in dev

Vladimir Kharlampidi
Sat, 26 Jul 2014 19:31:21 GMT

Already fixed in 0.9.4