[英]Ext 4.1 - How to get a reference to a class created with Ext.define()
I have a class being created with a method like this: 我有一个用这样的方法创建的类:
Ext.define('MY.class.Manager', {
....
....
....
manageStuff: function(){
}
}
Originally, I only needed the manageStuff
function in one place, so everything was fine. 最初,我只需要在一个地方放置
manageStuff
函数,因此一切都很好。 Now though, I have added another class and I need to be able to call the manageStuff
function from within the new class. 但是,现在,我添加了另一个类,并且需要能够从新类中调用
manageStuff
函数。
I have tried to get a reference in the new class: 我试图在新课程中获得参考:
var ManagerClass = Ext.getClass('MY.class.Manager');
And also: 并且:
var ManagerClass = Ext.getClass('class.Manager');
Both return null
两者都返回
null
Just to be sure that the Manager Class is being defined before I try to get it, I put in print statements: 为了确保在尝试获取Manager类之前已定义它,我输入了print语句:
They read as: 他们读为:
...making manager class
...getting manager class
Looking for any help because I am sure my approach to this problem is not even correct to begin with. 正在寻求任何帮助,因为我确信我解决该问题的方法一开始甚至都不正确。
To share behavioral traits between otherwise unrelated classes, use mixins: 要在其他不相关的类之间共享行为特征,请使用mixins:
Ext.define('MyApp.mixin.Foo', {
foo: function(bar, baz) {
alert(bar + ' ' + baz);
}
});
Ext.define('MyApp.class.Foo', {
mixins: [
'MyApp.mixin.Foo'
],
methodThatCallsFoo: function(bar, baz) {
this.foo(bar, baz); // foo method has been mixed in the
}
});
This applies to Ext JS 4.x+; 这适用于Ext JS 4.x +; bear in mind that in Ext JS 5 this mechanism became even more powerful, see Ext.Mixin doc .
请记住,在Ext JS 5中,此机制变得更加强大,请参见Ext.Mixin doc 。
You can make the method globally accessible, so both classes can use it. 您可以使该方法可全局访问,因此两个类都可以使用它。 First, if you don't have a global namespace already, you can create one with Ext using
Ext.namespace
. 首先,如果还没有全局名称空间,则可以使用
Ext.namespace
与Ext创建一个名称空间。 In this example, I will make MY
be the namespace all your global variables and methods will be contained in, and MY.helper
where your helper methods (like manageStuff
) will be contained in. 在此示例中,我将使
MY
成为包含所有全局变量和方法的名称空间,并将MY.helper
包含在其中的辅助方法(如manageStuff
)。
Ext.namespace(
'MY',
'MY.helper',
// more namespaces inside 'MY' as needed ...
);
And then you can declare you helper method: 然后可以声明您的辅助方法:
MY.helper.manageStuff = function() {
// do stuff...
};
And now you can use this method (and anything else stored inside MY
) anywhere in your application. 现在,您可以在应用程序中的任何位置使用此方法(以及存储在
MY
内的任何其他方法)。
Ext.define('MY.class.Manager', {
//...
//...
someFunctionThatCallsManageStuff: function(){
MY.helper.managerStuff();
}
});
Note: in javascript, you can store any variables inside the global namespace, and just call them directly, but I find that can clutter up the global namespace. 注意:在javascript中,您可以将任何变量存储在全局名称空间中,并直接调用它们,但是我发现这可能会使全局名称空间混乱。 Instead, I like to have a single variable that contains all my global variables.
相反,我希望有一个包含所有全局变量的变量。 In this example, your single variable is
MY
. 在此示例中,您的单个变量是
MY
。 Then you can add "packages" to it to organize your globals, such as MY.helper
. 然后,您可以向其中添加“包”以组织您的全局变量,例如
MY.helper
。
Note 2: Using Ext.namespace
in the example above is equivalent to the following: 注意2:在上面的示例中使用
Ext.namespace
等效于以下内容:
//init MY as global variable
MY = {};
// add properties to MY
MY.helper = {};
// add properties to MY.helper
MY.helper.manageStuff = function() {
// do stuff...
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.