-
17 May 2012 11:55 PM #1
[Ext 4.1.x] Proposal for listeners enhancement
[Ext 4.1.x] Proposal for listeners enhancement
I prefer to use ExtJS components in a declarative manner, but event binding is a bit tricky. Example:
This raises an error because no scope is given and existence of scope is not tested before accessing the method property. Adding scope: this to the listener declaration is not useful because this refers to globalWindow on construction time. So binding handlers to self is only possible by using initComponent() or another template function, which adds unnecessary code.Code:var cmp = Ext.create('Ext.button.Button', { text: 'foo', renderTo: Ext.getBody(), listeners: { afterrender: 'onAfterRender' }, onAfterRender: function() { console.log(this); } });
Proposal for a fix:
Code:Index: extjs-4.1.1-rc1/src/util/Observable.js =================================================================== --- extjs-4.1.1-rc1/src/util/Observable.js (revision ) +++ extjs-4.1.1-rc1/src/util/Observable.js (revision ) @@ -494,7 +494,7 @@ Ext.Error.raise('No method named "' + fn + '"'); } //</debug> - fn = scope[fn] || me[fn]; + fn = scope && scope[fn] ? scope[fn] : me[fn]; } event.addListener(fn, scope, Ext.isObject(options) ? options : {});Kind regards,
WillyDee
Problems worthy of attack, prove their worth by hitting back.
-
18 May 2012 3:02 AM #2
If the current behaviour is considered a bug, please move it to the appropriate forum. Thanks!
Kind regards,
WillyDee
Problems worthy of attack, prove their worth by hitting back.
-
25 May 2012 12:46 PM #3Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 34,118
- Vote Rating
- 453
IMO you should specify the function as the value in that button case.
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
2 Jun 2012 1:51 AM #4
Mitchell, this was not my intention. I want to be able to add a listener to any component, scoped to itself, but without the need to an extra call to the addListener() method. The code snippet is just a stripped example to show what I want to do, and where it fails. Please review the supplied patch; IMHO it's a small but useful enhancement, especially for MVC applications.
This is the procedural workflow as of today, binding the listener to the instance after the component is created:
In a MVC based app it would look like this:PHP Code:var cmp = Ext.create('Ext.button.Button', {
text: 'foo',
renderTo: Ext.getBody(),
onAfterRender: function() {
console.log(this);
}
});
cmp.addListener('afterrender', 'onAfterRender', cmp);
With the patch applied, the initComponent() call could be omitted, and the event binding would be completely declarative:PHP Code:Ext.define('App.view.MyButton', {
extend: 'Ext.button.Button',
text: 'foo',
onAfterRender: function() {
console.log(this);
},
initComponent: function() {
this.callParent();
this.addListener('afterrender', 'onAfterRender', this);
}
});
Got me?PHP Code:Ext.define('App.view.MyButton', {
extend: 'Ext.button.Button',
text: 'foo',
onAfterRender: function() {
console.log(this);
},
listeners: {
afterrender: 'onAfterRender' // automagically scoped to the component instance
}
});
Kind regards,
WillyDee
Problems worthy of attack, prove their worth by hitting back.
-
4 Jun 2012 11:01 AM #5
This trick may solve your problem:
PHP Code:Ext.define('App.view.MyButton', {
extend: 'Ext.button.Button',
text: 'foo',
onAfterRender: function() {
console.log(this);
},
listeners: {
scope: this, //All listeners will have the component as scope
afterrender: 'onAfterRender'
}
});
-
4 Jun 2012 2:19 PM #6
I don't think that will work, this will be the global window object, not the component.
Details aside, the underlying suggestion in this thread for allowing string handler names without an explicit scope is a good one. I definitely wouldn't call it a bug though, it's an enhancement.



Reply With Quote