2014-11-24 54 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