How to add a new method to Ext Core?
Could you please tell how to add a new method to Ext Core:
Ext.get('id').myMethod();
It does开发者_运维技巧n't work:
Ext.extend(Ext.Element, {
myMethod: function() {
this.on('click', function() {
alert(this);
});
}
});
(Assuming you are using ExtJS 3)
No, that's not the right way. We use extend
when we need to extend a Class and create our own class.
Then if you would like to add additional functionalities or override some of the core methods, use Ext.override
. This action is permanent and destructive since you can override any defined methods. We use override
when we want to apply personal fixes or global changes (like overriding Ext.form.Field
so to provide global, usable and cross fields
functionalities).
For your case, it seems like you want to apply this method to all the elements you have captured from Ext.get
. You will just need to override Ext.Element
.
Ext.override(Ext.Element, {
myMethod: function() {
this.on('click', function() {
alert(this);
});
}
});
An working example is given here: jsfiddle.
Note that Ext.get
is giving you Ext.Element
on a successful call. If you would like to create a customized Ext.Element
, be sure to modify Ext.get
so it will return your own customized Ext.Element
, or provides your own customized function to get elements.
Cheers!
精彩评论