简体   繁体   中英

Sencha touch better practice for off then on on events

I have a button in Sencha (touch) which in an action button. This button has a different function in each view. When I change the view the controller sets the correct function for the button:

myButton.on("click", function() {}); etc. 

When I switch view I first need to call:

myButton.off("click");

and then I can bind the click again with:

myButton.on("click", function() {});

If I not call the off function both click events will fire. Is there a faster way to bind a new event and flush all the other bound events?

You need to use the methods addListener and removeListener, for example:

var myFunction = function () { };
myButton.addListener("click", myFunction);
myButton.removeListener("click", myFunction);

For this to work, you need to pass the same function object to both methods, that means you can't declare the function inline when calling addListener.

To remove all handlers is very easy:

myButton.clearListeners();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM