PDA

View Full Version : Ext.ux.using



rstuven
30 Oct 2007, 2:57 PM
As a C# developer, I'm accustomed to the using directive (http://msdn2.microsoft.com/en-us/library/sf0df423%28VS.80%29.aspx) 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:



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...

});
When it comes useful, I start the scripts with this declaration. I adopted the "ns" convention as a shorthand for "namespace".

The code behind the using function is very simple:



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);
};
I hope you find this tip useful :)

andrei.neculau
1 Nov 2007, 8:22 AM
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?

rstuven
1 Nov 2007, 2:45 PM
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 (http://extjs.com/deploy/dev/examples/grid/grid3.html). That's the intent.

andrei.neculau
3 Nov 2007, 11:10 AM
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.

DragonFist
3 Nov 2007, 11:45 AM
@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.

andrei.neculau
4 Nov 2007, 12:15 AM
I was open to debate on a practical example, but instead I will explain my view on what it has already been written.


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...

});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.

Secondly, that code is only a rewrite of the following:


(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...
})();
Which would all be resumed by shortcuts to


// 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...
})();

I don't know - my organizing-focused mind will always keep to separate namespaces. Suite yourself if you find this useful.

jakeonthenet
4 Nov 2007, 1:41 PM
andrei,

Even though you don't see it, his extention is doing the exact same thing as your shortcut example.