-
30 Oct 2007 2:57 PM #1
Ext.ux.using
Ext.ux.using
As a C# developer, I'm accustomed to the using directive that brings types from other namespaces to the current scope, so I came up with a handy function for my Ext projects to emulate "using" at some extent:
When it comes useful, I start the scripts with this declaration. I adopted the "ns" convention as a shorthand for "namespace".Code:Ext.ux.using( Ext.data, Ext.form, Ext.grid, Ext.ux.grid.filter, {ShortAlias: Ext.long.path.to.SomeVeryLongClassName}, function(ns) { // Some code... var g = new ns.GridPanel(); var r = new ns.JsonReader(); var t = new ns.TextField(); var f = new ns.GridFilter(); var a = new ns.ShortAlias(); // More code... });
The code behind the using function is very simple:
I hope you find this tip usefulCode:Ext.ux.using = function() { var ns = {}; var upper = arguments.length - 1; for (var i = 0; i < upper; i++) { var source = arguments[i]; Ext.apply(ns, source); } arguments[upper](ns); };
Last edited by rstuven; 30 Oct 2007 at 5:00 PM. Reason: Grammar
-
1 Nov 2007 8:22 AM #2
I'm having difficulties following your code.
So basically.. what you are doing is to apply procedures and properties from different namespaces to one namespace, thus merging them.
Why would that be any good? Can you give a practical example?
-
1 Nov 2007 2:45 PM #3
Yes, it's as simple as you put it: merging different namespaces to one.
The benefits are the same of the C# using directive (see link above) or import in Java and Python, or similar constructs in other languages: less typing in a structured way.
A pratical example? See how is used the xg variable in the official Grid3 example. That's the intent.
-
3 Nov 2007 11:10 AM #4
I'm sorry to say that namespaces do have their purpose, while you are taking away that purpose. Just my opinion - open to debate on a very practical and useful example.
xg in the Grid3 example is only a shortcut to Ext.grid, which thus has no connection to your idea.
-
3 Nov 2007 11:45 AM #5
@andrei.neculau
I cannot agree with you there. He's taking nothing away from the purpose of Namespaces AND what he is specifically doing is giving an easy way to create shortcuts to those namespaces.
-
4 Nov 2007 12:15 AM #6
I was open to debate on a practical example, but instead I will explain my view on what it has already been written.
First of all, I added Ext.ux.form (which we suppose to have Ext.ux.form.ComboBox) which would overwrite Ext.form.ComboBox - of course it would only happen if we are not paying attention, etc.Code:Ext.ux.using( Ext.data, Ext.form, Ext.grid, Ext.ux.form, Ext.ux.grid.filter, {ShortAlias: Ext.long.path.to.SomeVeryLongClassName}, function(ns) { // Some code... var g = new ns.GridPanel(); var r = new ns.JsonReader(); var t = new ns.TextField(); var f = new ns.GridFilter(); var a = new ns.ShortAlias(); // More code... });
Secondly, that code is only a rewrite of the following:
Which would all be resumed by shortcuts toCode:(function(){ // Some code... var g = new Ext.grid.GridPanel(); var r = new Ext.data.JsonReader(); var t = new Ext.form.TextField(); var f = new Ext.ux.grid.filter.GridFilter(); var a = new Ext.long.path.to.SomeVeryLongClassName(); // More code... })();
I don't know - my organizing-focused mind will always keep to separate namespaces. Suite yourself if you find this useful.Code:// code... var EG = Ext.grid; var ED = Ext.data; var EF = Ext.form; var uEF = Ext.ux.form; var uEG = Ext.ux.grid; var uAlias = Ext.long.path.to; // code... (function(){ // Some code... var g = new EG.GridPanel(); var r = new ED.JsonReader(); var t = new EF.TextField(); var f = new uEG.filter.GridFilter(); var a = new uAlias.SomeVeryLongClassName(); // More code... })();
-
4 Nov 2007 1:41 PM #7
andrei,
Even though you don't see it, his extention is doing the exact same thing as your shortcut example.


Reply With Quote


