PDA

View Full Version : Extending Singletons with private scope in Ext 4?



chris.wood
19 Sep 2012, 12:29 PM
In the course of an ExtJS 3 to 4 migration, I have run into a snag I'm wondering if anyone has tackled: namely, how can I extend a singleton with private scope?

In Ext3 I would do this using Extend:

Ext.namespace("My.New.Obj");

My.New.Obj = (function() {
var privateVar = 3;
function privateFunc() { alert(privateVar); }

var extendedObj = Ext.extend(My.Other.Obj, {
newFunc: function() { alert(this.publicVar+privateVar); },
publicVar: 4
});

return new extendedObj();
})();

As best I understand, I would create a singleton pattern using Ext.define but I don't know how to extend the internal object.


Ext.define('My.New.Obj', function() {
var privateVar = 3;
function privateFunc() { alert(privateVar); }

var extendedObj = Ext.create('My.Old.Obj',{
newFunc: function() { alert(this.publicVar+privateVar); },
publicVar: 4
});

return extendedObj;
});

The only trouble with the above example is that I believe the super methods that were preserved in Ext.extend get overridden.

How can I extend a singleton while keeping private scope?

evant
19 Sep 2012, 3:34 PM
Answered this on stack overflow, but for others interested:



Ext.define('A', {
someMethod: function(){
return 'a';
}
});

Ext.define('B', (function(){
var fn = function(){
return 'b';
};

return {
extend: 'A',

someMethod: function(){
return this.callParent() + fn();
}
}
})());

console.log(new A().someMethod());
console.log(new B().someMethod());

chris.wood
20 Sep 2012, 4:10 AM
Appreciate the fast response. Thanks!