2014-11-24 77 views
2

我使用的是ExtJs 5和SenchaCmd。用ExtJs5定义名称空间

在一个包,我在几个文件中定义的几类这样的:

// Bar1.js 
Ext.define('Foo.Bar1', { 
    ... 
}); 

// Bar2.js 
Ext.define('Foo.Bar2', { 
    ... 
}); 

现在,我想只是“增加”的富命名空间与一些一般工具是这样的:

Ext.ns('Foo'); 
Foo.tool1 = function() { ... } 
Foo.tool2 = function() { .... } 
Foo.generalProp1 = 42; 
(...) 

为了让Sencha编译器嵌入这个文件,声明这个更好的地方是什么?

是否有可能“需要”(以某种方式)像我们需要一个类的名称空间?

回答

3

您可以使用statics从新分机:

Ext.define('Foo', { 
    // declare static members 
    statics: { 
     method: function(){ return "static method"; } 
    } 
}); 

// Define class under Foo namespace 
Ext.define('Foo.OtherClass', { 
    method: function(){ return "instance method"; } 
}); 

// Create instance of Foo.OtherClass 
var o = Ext.create('Foo.OtherClass'); 

// Use static member 
console.log(Foo.method()); 

// Use instance member 
console.log(o.method()); 

然后你就可以把你的Foo命名空间像类,地点和其他类。 那么显然你也可以要求这个命名空间,因为它也是类。

+0

谢谢。我有两个问题: 1 /我必须在同一个文件中声明这两个命名空间('Foo'和'Foo.OtherClass')吗? 2 /如果我想在“Foo”命名空间中添加Foo.OtherClass的一些函数快捷方式,它是否很复杂? – Guid 2014-11-25 16:26:47

+2

1.否 - 在Foo/OtherClass.js中的/Foo.js和OtherClass中定义Foo; 2.您可以将回调传递给在创建类后调用的'Ext.define',例如:'Ext.define('Name',{},function(nameClass){})'。在此回调中,您可以创建快捷方式。 小提琴:http://jsfiddle.net/r59wopn5/2/ – Krzysztof 2014-11-26 07:39:09

+0

很好的答案!非常感谢。 – Guid 2014-11-26 22:24:56

相关问题