admin管理员组文章数量:1328609
How do I reference an instance of a custom class inside a custom function defined within the class?
I've extended a class through ExtJS4's class extension "mechanism", and I've a custom event handler that will be called when something is triggered, and I want to collapse the Panel when something is fired.
However, in the event handler of the Reactor class below, "this" references EventTriggerer (The firer of the event) instead of the instance of the Reactor. How do I reference the instance of the EventReactor inside a custom function?
Thanks! DashK
Ext.define('EventReactor', {
extend: 'Ext.panel.Panel',
onSomething: function(data) {
// ERROR
// this.collapse is undefined. "this" actually references EventTriggerer
this.collapse();
}
});
Ext.define('EventTriggerer', {
extend: 'Ext.util.Observable',
singleton: true,
constructor: function() {
this.addEvents({
"changedView" : true
});
},
doSomething: function() {
// Do some stuff
this.fireEvent("doneSomething", data);
}
});
...
// Here's how the listeners are added
var er = Ext.create('EventReactor');
EventTriggerer.addListener("doneSomething", er.onSomething);
// Here's how the event is triggered.
er.doSomething();
How do I reference an instance of a custom class inside a custom function defined within the class?
I've extended a class through ExtJS4's class extension "mechanism", and I've a custom event handler that will be called when something is triggered, and I want to collapse the Panel when something is fired.
However, in the event handler of the Reactor class below, "this" references EventTriggerer (The firer of the event) instead of the instance of the Reactor. How do I reference the instance of the EventReactor inside a custom function?
Thanks! DashK
Ext.define('EventReactor', {
extend: 'Ext.panel.Panel',
onSomething: function(data) {
// ERROR
// this.collapse is undefined. "this" actually references EventTriggerer
this.collapse();
}
});
Ext.define('EventTriggerer', {
extend: 'Ext.util.Observable',
singleton: true,
constructor: function() {
this.addEvents({
"changedView" : true
});
},
doSomething: function() {
// Do some stuff
this.fireEvent("doneSomething", data);
}
});
...
// Here's how the listeners are added
var er = Ext.create('EventReactor');
EventTriggerer.addListener("doneSomething", er.onSomething);
// Here's how the event is triggered.
er.doSomething();
Share
Improve this question
edited Jun 22, 2011 at 15:06
JamesHalsall
13.5k4 gold badges43 silver badges67 bronze badges
asked Jun 22, 2011 at 14:23
DashKDashK
2,6801 gold badge22 silver badges28 bronze badges
2 Answers
Reset to default 9You should create a nested scope to store "this" during constructor execution:
Ext.define('EventReactor', function() {
var self; // This is a variable to store "this"
return {
extend: 'Ext.panel.Panel',
constructor: function() {
self = this; // Here you store "this" in the closure
self.callParent(arguments);
},
onSomething: function(data) {
// ERROR
// this.collapse is undefined. "this" actually references EventTriggerer
// this.collapse();
// Using "self" instead of "this" => No error
self.collapse();
}
};
});
Detailed information (see define method): http://docs.sencha./ext-js/4-1/#!/api/Ext
The addListener() method has an additional parameter scope
that can be set to an object that bees this
in the event handler.
EventTriggerer.addListener("doneSomething", er.onSomething, er);
本文标签: javascriptExtJSHow to reference quotselfquot in a custom function in a custom classStack Overflow
版权声明:本文标题:javascript - ExtJS - How to reference "self" in a custom function in a custom class? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742235235a2437957.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论