fred basset
fred basset

Reputation: 10092

How to JSON serialize an fx:Model

I'm testing out using fx:Models, serializing it to JSON is not working as expected however. In the example below I do not get the model's data in the JSON, all I get is the uuid. How can I serialize models to JSON cleanly?

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)">
<fx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import mx.events.FlexEvent;

        protected function application1_creationCompleteHandler(event:FlexEvent):void
        {
            mod1.part = "Initial value";
        }

    ]]>
</fx:Script>
<fx:Declarations>
    <fx:Model id="mod1">
        <data>
            <part>{ti.text}</part>
        </data>
    </fx:Model>

</fx:Declarations>
<s:TextInput id="ti" x="98" y="155" text="{mod1.part}">
</s:TextInput>
<s:Button x="120" y="193" label="Read from model" click="Alert.show(mod1.part, 'Model Data')"/>
<s:Button x="120" y="220" label="Model as JSON" click="Alert.show(JSON.stringify(mod1), 'Model as JSON')"/>

Upvotes: 0

Views: 335

Answers (1)

Sam DeHaan
Sam DeHaan

Reputation: 10325

I was able to serialize the data as JSON by changing your stringify call to...

JSON.stringify(mod1.valueOf())

Upvotes: 1

Related Questions