简体   繁体   English

ExtJS - 如何保留“this”类对象?

[英]ExtJS - How to keep “this” class object?

my code in a Controller class of ExtJs looks like this below. 我在ExtJs的Controller类中的代码如下所示。

  1. How do I pass the this object to other functions without using global variables, since I will use multiple instances of my app. 如何在不使用全局变量的情况下this对象传递给其他函数,因为我将使用我的应用程序的多个实例。

  2. If it can only be done globally, how can I isolate my global variables (only for one instance)? 如果它只能在全局范围内完成,我如何隔离我的全局变量(仅针对一个实例)?

I use ExtJs 4.1 and DeftJs 我使用ExtJs 4.1和DeftJs

Thanks! 谢谢!

#

Edit: 编辑:

I want to replace oMeasurements with this in all places ! 我想在所有地方用this替换oMeasurements

 init: function() { oMeasurements = this; this.readConfig(); }, readConfig: function() { oMeasurements.httpApi.request({ module : 'monitor', func : 'getConfig', success: oMeasurements.readConfigCallback }); }, readConfigCallback: function(oResponse) { taskMeasurements = { run: oMeasurements.output, interval: '1000' ,scope: this } Ext.TaskManager.start(taskMeasurements); }, output: function() { // finally here, "this" is no more my original "this" console.log(this); oMeasurements.httpApi.request({ module : 'monitor', func : 'getMeasurementsFiles', success: oMeasurements.outputCallback }); } 

You need to add scope: this after callback definition. 您需要添加scope: this在回调定义之后。 You're losing scope not because you're calling function from another function, but because you're using callbacks. 你失去范围不是因为你从另一个函数调用函数,而是因为你正在使用回调。

Usually there is a scope parameter in Ext function calls with callbacks. 通常在具有回调的Ext函数调用中有一个scope参数。

You can also set "this" of a function using Ext.bind(); 您还可以使用Ext.bind()设置函数的“this”;

For example, if you passed a function a() to a another method and ran it, "this" would be the callee. 例如,如果您将函数a()传递给另一个方法并运行它,那么“this”就是被调用者。 If you called Ext.bind(a, this), when passing "a", "this" would refer to the caller. 如果你调用Ext.bind(a,this),当传递“a”时,“this”将引用调用者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM