PDA

View Full Version : How to pass object by reference to other components



forumuser1080
9 Jul 2011, 12:57 PM
I have a 'wizard' that is a card layout. The wizard constructs and object as the user progresses through. At the end the object is submitted to the server.

I want the wizard to create a new object and give each 'card' a reference to that object so that each card can add the appropriate info to the object. The last card will submit the object.

I am trying the following, but I am getting a deep copy of my object in every card so when each card adds some info the next card does not have that info (each card has their own instance).



MyApp.Wizard = Ext.extend(Ext.Panel, {
constructor: function() {
// init myObj to an empty object and try and share reference with other cards
this.myObj = {};
MyApp.Wizard.superclass.constructor.apply(this, arguments);
},

initComponent: function() {
Ext.apply(this, {
layout: 'card',
activeItem: 0,
defaults: {
// Give each one of my cards the object
// Problem is this is a copy of this.myObj
// is there anyway to give them all the reference not a copy
myObj: this.myObj
},
items: [
new MyApp.Card1(),
new MyApp.Card2(),
new MyApp.Card3()
]
});
}

Any ideas??

forumuser1080
9 Jul 2011, 1:40 PM
Sorry for the confusion but that seems to work just fine. My problem was that in my first card I have a combo box for a type of object that is going to be created:



// Card 1 function
onTypeSelection: function(combo) {
this.myObj = {
myType = combo.getValue();
};
}
This is essentially resetting Card1's reference to the object that was initially created.

SorryI was thinking that I could clear out myObj and all other components that had a handle on myObj would still have that handle but it would be this new 'reset' object.

Which leads to the problem of when my type switches how do I start over with a new object and still have the other components still reference the orignal object that was created by the wizard?

skirtle
10 Jul 2011, 5:19 PM
Which leads to the problem of when my type switches how do I start over with a new object and still have the other components still reference the orignal object that was created by the wizard?

Could you rephrase the question?

As far I can tell you have 2 choices for setting myType:


this.myObj = {
myType: ...
};


this.myObj.myType = ...;

The first case will create a new object that the other cards know nothing about, the second will change the relevant property of the shared object. I can't figure out which one it is you want.

forumuser1080
11 Jul 2011, 3:25 PM
Yeah sorry for the confusion.

I want to clear all properties from myObj, but I still want all the other components to have the correct handle on myObj.

I thought the easiest way was to just do:


myObj = {
type = .....
}


But as you pointed out that creates a new obj that now none of the other cards know about.

Since I still need to clear all the properties I came up with:


for (i in this.myObj) {
if (this.myObj.hasOwnProperty(i)) {
delete this.myObj[i];
}
}

this.myObj.type = newType;