PDA

View Full Version : Problem with Ext.override - creates global property



PICS
1 Dec 2009, 3:37 AM
I use the same form with different records in different Tabs, like:
edit record 1 goes to tab_edit_1
edit record 2 goes to tab_edit_2

to get rid of all the elements in the form ( subforms, grids, trees ) I made a small extension to add references to an element like:
tab_edit_1 = subform_1_1, subform_2_1
tab_edit_2 = subform_1_2, subform_2_2

here my extension:


Ext.override(Ext.util.Observable, {

linkedObjects: new Object()

/**
* add someting to the references
*/
,
addRef: function (pID, pObject) {
this.linkedObjects[pID] = pObject;
} // addRef
/**
* get a Reference back
*/
,
getRef: function (pID) {
return this.linkedObjects[pID];
} // getRef

first all looks fine, but now I found, that all objects share the same global content in the linked objects, like:


Ext.override(Ext.util.Observable, {

linkedObjects: new Object()

/**
* add someting to the references
*/
,
addRef: function (pID, pObject) {
this.linkedObjects[pID] = pObject;
} // addRef
/**
* get a Reference back
*/
,
getRef: function (pID) {
return this.linkedObjects[pID];
} // getRef
});

Ext.onReady(function () {

var mainform1 = new Ext.FormPanel({
title: 'form1'
});
var mainform2 = new Ext.FormPanel({
title: 'form2'
});

mainform1.addRef('sub1', 'subform11');
mainform1.addRef('sub2', 'subform12');
mainform2.addRef('sub1', 'subform21');
mainform2.addRef('sub2', 'subform22');

// will alert subform21 instead of subform11
alert(mainform1.getRef('sub1'));

}); // Ext.onReady

now mainform1 should have 2 references sub1 and sub2 to subform11 and subform12
and mainform2 should have 2 references sub1 and sub2 to subform21 and subform22

but mainform1, mainform2, subform11, subform12, subform21 and subform22
has the references sub1 and sub2 to subform21 and subform22

I'm a little bit blocked, where I go wrong, hope someone can help me

VinylFox
1 Dec 2009, 4:23 AM
Just like your addRef and getRef methods, linkedObjects is shared between all instances of that class when its placed on that main level of the object.

What you want to do is change linkedObjects to be within the instances scope.

Here is one way you could go about doing that:

Ext.override(Ext.util.Observable, {
addRef: function (pID, pObject) {
if (!this.linkedObjects) this.linkedObjects = {};
this.linkedObjects[pID] = pObject;
return pObject; // return the object so you dont break chaining
},
getRef: function (pID) {
if (!this.linkedObjects) this.linkedObjects = {};
return this.linkedObjects[pID];
}
});

PICS
1 Dec 2009, 5:44 AM
thanks, this works fine.

regards, christian