PDA

View Full Version : mon/mun vs. on/un



Eric24
21 Oct 2010, 6:22 AM
I know there is a good write-up somewhere of when to use mon vs on, but I can't find it. Can anyone point me to it?

StuartAshworth
21 Oct 2010, 7:28 AM
Saki posted a good wee article about them last year...

What the hell is mon and mun? (http://blog.extjs.eu/know-how/what-the-hell-is-mon-and-mun/)

Eric24
21 Oct 2010, 8:02 AM
Yes, that's what I was looking for--thanks! Now that I've re-read it, I still think I'm a little confused of exactly when it is necessary to use mon vs on. I seem to remember something about ExtJS automatically removing listeners from some elements and not others. As I understand it, if I add a listener directly to the component itself, I can use on, but if I add a listener to some child of the component, I should use mon. Is that even close to being right?

Condor
22 Oct 2010, 5:03 AM
If you use 'mon' then Ext will automatically remove the element listener when the component is destroyed.

If you use 'on' then you will have to write your own code to remove the element listener when the component is destroyed.

nosferatum
22 Oct 2010, 5:19 AM
As Saki says in the end of the article:

"Warning! mon and mun are not documented (yet?) so there is still (a little) risk that they will change or will be removed."

Is it guaranteed that mon\mun features would not be removed in next releases of Ext?

Uberdude
22 Oct 2010, 5:20 AM
A use case of mon is when your component wants to listen to the events of some singleton service whose lifetime is greater than yours. So lets say we have a WidgetService that lasts for the lifetime of the app, and we make a WidgetGrid to display the widgets. WidgetService fires a widgetcreate event when someone makes a new widget and we want the grid to auto-refresh. This should be done with


widgetGrid.mon(WidgetService, 'widgetcreate', refreshGrid, widgetGrid /* scope for refreshGrid */)

This way when the widget grid is destroyed the listener is removed. WidgetService still exists. If we just used 'on' then the listener would still be there and the next time the service fired the widgetcreate event the handler would explode as the grid no longer exists. In Ext 2 you could solve this by 'un'ing the listener when the widget grid is destroyed, but that's extra typing and easy to forget.

Uberdude
22 Oct 2010, 5:21 AM
They are now documented: http://dev.sencha.com/deploy/dev/docs/?class=Ext.Component

Eric24
22 Oct 2010, 5:30 AM
If you use 'mon' then Ext will automatically remove the element listener when the component is destroyed.

If you use 'on' then you will have to write your own code to remove the element listener when the component is destroyed.

Yes, that much is clear. What's not clear is that some Ext components are written in such a way that they will automatically remove listeners, even those added "later" with 'on' (like doing a removeAll on destroy). Maybe there's no way to know except to look at the code for the specific component?

Condor
22 Oct 2010, 5:34 AM
The Ext library still contains a lot of old code that hasn't been rewritten yet to take advantage of 'mon'.