javascript - Remove an event listener from a component only based off of its function name - Ext Js -
alright have viewport divided 2 halves, top , bottom. top half has combo boxes , input fields visible, bottom half has tab panel multiple tabs.
when change 1 of static combo boxes on top half, i'll call combo box a
, fires event , in event handler, a_handler
, attempt style combo box in 1 of tabs, i'll call combo box b
, may 1 of 2 things.
combo box b
visible on tab 2
, not 1
.
so then:
1) if tab 1
active, setup event handler style combo box b
when tab 2
made active.
2) otherwise, style it.
so here's problem: initial combo box, a
, can style combo box b
1 of 2 possible ways, depending on selection, red
, blue
.
so imagine situation tab 1
visible, , select option style b
red
, event handler created when tab 2
activated. then, without activating tab 2
, change option again in combo a
, make box b
blue
.
at point, need remove first event handler 1 fired.
i not have reference event handlers, have named them.
i have created functions acquire list of event listeners on tab 2
, , know name of listener remove, can match names, scopes not same when attempt remove them tab's removelistener
, nothing happens.
solved
eventually found solution this.
the un
or removelistener
functions setup this:
un( eventname, fn, [scope] )
my problem did not have reference specific handler pass in fn
the solution involves finding fn
ref searching fn name.
so when event listener created, setup name:
mytab.on('activate', function dosomestuff() {
later on when need check if exists/possibly remove it:
objecthasnamedlistener: function(obj, eventtype, name) { var listeners = obj.events[eventtype].listeners; //trim functionality since .trim() doesn't exist in old ie name = name.replace(/^\s+|\s+$/g, ''); if (listeners != undefined) { (var = 0; < listeners.length; i++) { var func = listeners[i].fn; var funcstr = func.tostring(); var funcname = funcstr.substr('function'.length); funcname = funcname.substr(0, funcname.indexof('(')); funcname = funcname.replace(/^\s+|\s+$/g, ''); if (funcname == name) { return true; } } } return false; },
and if returns true, reference handler:
if (this.objecthasnamedlistener(mytab, "activate", "dosomestuff")) { var listener = this.getnamedlistener(mytab, "activate", "dosomestuff"); mytab.removelistener("activate", listener, this); } getnamedlistener: function(obj, eventtype, name) { var listeners = obj.events[eventtype].listeners; name = name.replace(/^\s+|\s+$/g, ''); if (listeners != undefined) { (var = 0; < listeners.length; i++) { var func = listeners[i].fn; var funcstr = func.tostring(); var funcname = funcstr.substr('function'.length); funcname = funcname.substr(0, funcname.indexof('(')); funcname = funcname.replace(/^\s+|\s+$/g, ''); if (funcname == name) { return func; } } } return null; },
the other caveat listener had created , removed same scope passed in, despite fact listed [optional] in api, passing in worked fine
Comments
Post a Comment