Thanks for you thought on this. Actually the only reason that I need a copy of the object is to achieve the binding from the text field back to the data model.

I created a simple function to bind the text field to a data model:

private function bindToInput(event:FlexEvent):void{     
BindingUtils.bindProperty(model.contactState.newContact, String (event.target.id.substring(3)), event.target, "text");
}

Then I declare my text field like this:

<controls:PromptingTextInput id="id_firstName" prompt="First" text="{model.contactState.pendingContact.firstName}" initialize="bindToInput(event);"/>

I just need to make sure that the name of the text field (after the id_) is the same as the property on the model and then everything binds when the text field is initialized.

This actually work great...so far. I tried out the ObjectUtil.copy() method (suggested by Alex) and it seems to do the trick in copying my object so that I can achieve this type of Binding. I am going to test it out some more and I'll post if I have any problems. My other solution was to create some "hidden" text fields that hold the non- editable properties that come from the server (like id) and then bind them the same way as above.

Thanks for the advice!!

- Kevin

On Mar 26, 2007, at 4:07 PM, simonjpalmer wrote:

You are right, it is because your model.pendingContact and
model.newContact are object references and not object instances. When
you do the assignment you are just pointing them to the same object in
memory - 2 references, 1 object. In your second case you create a
second object, 2 objects, 2 references. This is a familiar concept if
you have spent any time coding in Java.

I'm willing to bet you won't find a cloning technique that will really
suit your purposes (but then I enjoy a flutter). From personal
experience I am yet to find a use for cloning that I have stuck with.
I have used it in Java but every time I have thought it was a
solution it turned out not to be because of some logical constraint in
the data. Most likely there will be some logic implicit in the
member variables or your object (such as id's etc) that it would not
be safe to simply clone. If you have a complex object containing an
array what would you expect it to do? I generally resort to
intelligent copying and a self-impose standard interface across my
code to copy (I used to use overloaded = operators and copy
constructurs in C++ but that's the very distant past).

For the case that you cite you must be binding the individual members
of your VO to form fields to display their values. One approach is to
trap the <enter> key on those fields and apply the new values back to
the object. That way you only need a single object and its state is
represented by the fields in your form...

<mx:TextInput id="txt_Name" text="{myVOContact.name}" enter="saveName()"/>
...
private function saveName():void{myVOContact.name=txt_Name.text;}
...

You might wind up with lots of little saveX() functions but that
wouldn't be the end of the world.

Alternatively you could probably also bind the assignment directly...

<mx:TextInput id="txt_Name" text="{myVOContact.name}"
enter="{myVOContact.name=txt_Name.text}"/>

Not every control support the enter event but there is normally some
analogue, such as a combo closing or a checkbox state changing or a
radio button being selected that will do just as well.

A third method involves putting a submit button on the page. When
clicked, manually transfer the data from the form fields to your VO
and post back to the server.

Unless you are measuring the changes in your data I'm not seeing the
value of a second object instance (yippee, no clone).

--- In flexcoders@yahoogroups.com, Kevin <[EMAIL PROTECTED]> wrote:
>
> i think my problem boils down to one of object cloning. Does anyone
> have a good solution for deep cloning objects in AS3? Seems like
> this would be a pretty useful utility.
>
> - Kevin
>
>
>
> On Mar 26, 2007, at 1:50 PM, Kevin wrote:
>
> > I have trying to figure out how best to accomplish the two-way data
> > binding needed for form entry. Here is what I thought would work,
> > but it doesn't seem to:
> >
> > ON MODEL:
> > I created two variables that hold my VO.
> >
> > model.pendingContact
> > model.newContact
> >
> > The user clicks a button to get ContactVO data from the server. In
> > the command class I assign the result (which is a typed PHP
> > ContactVO) to each variable:
> >
> > model.pendingContact = data.result;
> > model.newContact = data.result;
> >
> > I then bind my form fields to "model.pendingContact" so that the
> > existing values from the server show up in the TextInput fields.
> >
> > Lastly, I use the BindingUtil methods to bind the "text" property of > > the TextInput fields back to "model.newContact" so that I can record
> > any changes and send them back to the server.
> >
> > When the user submits the form, "model.pendingContact" should hold
> > the old values & "model.newContact" should have the new values.
> > Correct??
> >
> > Am I overlooking something. The only way I can get this to work is
> > if I don't assign "model.newContact" to the values from the server
> > and instead create a new instance like this:
> >
> > model.pendingContact = data.result;
> > model.newContact = new ContactVO;
> >
> > However, I can't do this because I need the newContact to start out
> > with the old values from the server. Is there a duplicate or clone
> > object method that I need to use so that both model variables can be
> > assigned to the same object from the server??
> >
> > Thanks for the help.
> >
> > - Kevin
> >
> >
>




Reply via email to