PDA

View Full Version : Single fire event listener, config defined style vs. .on() / .un()



Csegota
2 Jul 2014, 1:42 PM
I'm having problems getting a tap.Panel to set the default tab to the one in index 0. For whatever reason, setting the activeTab config is having no effect. I've tried numerous event listeners and the only one that works is afterlayout. The thing about that is(and I'm kinda just assuming here), with that listener I now cant navigate away from the 0 tab. I'm assuming that every time I hit another tab, its calling afterlayout, and then setting it back to the 0 tab.

So in my tab.Panel definition, I want to do something like this:


listeners: {
afterlayout: function() {
this.setActiveTab(0);
this.un('afterlayout', fn??);
}
},


My question is, how do I set the fn reference that is required for unregistering listeners?

Maybe this is the wrong way to do this.

I know I could just add a listener right before I show this panel using '.on()' and '.un()', which I may do, but I was curious if the first solution was possible or not recommended?

Would below be the best way to go about this single fire listener task?


var fn = function() {
tabView.setActiveTab(0);
tabView.un('afterlayout', fn);
}
tabView.on('afterlayout', fn);

Bonus curiosity question...

What if I did it like this:


tabView.on('afterlayout', function() {
tabView.setActiveTab(0);
tabView.un('afterlayout', ???); <---
});

How would I reference that function that is directly passed when I want to remove the listener from within the function itself?

Thanks!

evant
2 Jul 2014, 1:51 PM
Trying to do it in afterlayout is the wrong spot, activeTab will kick it off, but you didn't post any code so it's not really possible to say.

As for a single fire listener, an option already exists to do that: http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.util.Observable-method-addListener

Csegota
2 Jul 2014, 2:33 PM
I wish it was possible to post a consolidated chunk of code that defines this window. However there is literally hundreds of lines of code that set up this one window. That is why I tried to define my problem in a concise way. When you say afterlayout is the wrong spot to do this; what event would you say is the best place to do such a task?(1)

I just realized that afterlayout fires 27 times during the rendering of that panel... is that a bad sign? or is it common for that event to fire multiple times during the rendering of a panel?(2)

When you say activeTab will kick it off, what do you mean exactly?(3)


but you didn't post any code so it's not really possible to say.

What exactly is not really possible to say? ( why activeTab config isn't working as expected? what method of attaching a listener is best for this scenario? one of my many questions on listener preferred syntax?)(4)

Thanks!

evant
2 Jul 2014, 3:56 PM
It's more or less the XY problem: http://mywiki.wooledge.org/XyProblem

You should put together a test case that demonstrates the problem so that the activeTab can be set without any extra work. In creating a simplified test case, it's quite possible that you discover the issue that is preventing activeTab from functioning.

Otherwise, as I said above, you can use the single: true option on a listener.