What the hell is mon and mun?
18. September 2009 – 02:31I’ve run across the following code in some of Ext 3.x examples:
Looks like event handler installation but what it really does anyway?
So I delved into the code and remembered ExtJS Conference and now it is clear to me.
Imagine you create a component and then you need to install an event handler, for example, on its body. Something like:
Easy enough, we’ve done something like this many times. But, if the panel is ever to be destroyed we need to remove this listener ourselves as Ext knows nothing about it.
Something like:
If we install our listener as inline function, such as:
then it is impossible to remove this listener selectively, you would need to use
to remove all body listeners.
Now, if we use mon (m stands for “managed”) this way
then the listener is automatically removed by Ext on panel destroy. Nice, isn’t it? Saves a lot of our work and also handles our laziness or forgetfulness about destroying and cleanups of components.
Mind the first argument, that is the element to install the listener to. It can, but not necessarily have to be within the component. Also remember that when you destroy p Ext only removes listener from the element so if the element is outside of p you need to remove it yourself if it is not necessary anymore.
Should you ever need to remove the listener installed by mon you use mun with same arguments. You cannot selectively remove inline listeners.
mon and mun are defined in Component so you can use them from Component down the descendant classes chain.
本文解释了Ext JS库中的mon和mun方法在事件处理中的应用,以及它们如何帮助自动化组件事件监听器的安装和卸载,节省开发者的工作量。

被折叠的 条评论
为什么被折叠?



