PDA

View Full Version : Standard class definition patterns



shousper
15 Mar 2011, 10:39 PM
Hi,

I'm still really new to ExtJS, and have been playing around with different standard patterns to defining classes, etc.

I'm finding that my application is going to use a lot of class name and/or namespace name referencing, so I've come up with this:


(function()
{
var $ns = 'Example.infra';
var $class = 'Tiger';
var $fullclass = $ns + '.' + $class;

var $self = Ext.ns($ns)[$class] = Ext.extend(Example.Rawr,
{
blah: 'blah',

initComponent: function()
{
// Other component stuff here..
// Using $fullclass or $class..

// Call superclass impl.
$self.superclass.initComponent.call(this);
}, // eo initComponent
});

})();

It seems really clunky to me, but satisfies my desire to not have magic literals everywhere.

Has anyone got any views? Opinions? Better ideas for this sort of situation?


Cheers

Barzoy
16 Mar 2011, 3:43 AM
You can make it this way. But in this case MyPanel souldn't have any child classes, in other words, MyPanel should always be a final class.

var MyPanel = Ext.extend(Ext.Panel, {
constructor: function() {
this.constructor.superclass.constructor.apply(this, arguments);
},
initComponent: function() {
this.constructor.superclass.initComponent.apply(this, arguments);
}
});

This way helps if your class is not final

(function(R) {
R = Ext.extend(Ext.Panel, {
constructor: function() {
R.superclass.constructor.apply(this, arguments);
},
initComponent: function() {
R.superclass.initComponent.apply(this, arguments);
}
});
})(Ext.namespace('Ext.YourAppName.MyPanel'));

shousper
16 Mar 2011, 4:47 PM
Hmm, both interesting approaches, thanks for sharing! =) I really need a way to get the namespace, class, and fully qualified class name though. I think the $self was just an extra lazy way I could avoid typing the class name every time, plus if I needed to copy/paste code, or use another class as a template it saved a find & replace.

My earlier approach was similar to this (http://www.jasonclawson.com/2008/05/28/extjs-using-namespaces-improved/), but without the overrides, but again, it's the _names_ I need constantly.