tags:

views:

472

answers:

3

In MXML it's possible to declare object instances, even for dynamic objects, like so:

<mx:Object>
    <mx:foo>bar</mx:foo>
    <mx:bar>foo</mx:bar>
</mx:Object>

Is it possible to do the same with the Dictionary class without using some MXML wrapper class?

A: 

No, I don't think Dictionary is bound to MXML.

Do you really need a Dictionary for what you want to accomplish?

CookieOfFortune
+4  A: 

I'm not completely sure I understand the question, but if you're asking whether you can declare a dictionary in MXML, yes, you can; here's some code demonstrating a Dictionary declared in MXML, along with a form showing how you might add items to the dictionary dynamically:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" xmlns:utils="flash.utils.*" creationComplete="onCreationComplete()">

    <mx:Script>
     <![CDATA[

      private function onCreationComplete():void
      {
       showContents();
      }

      private function onBtnClick():void
      {
       addProperty();
       showContents();
      } 

      private function showContents():void
      {
       txt.text = "";

       for (var k in d) 
        txt.text += k + ": " + d[k] + "\n"
      }

      private function addProperty():void
      {
       d[pname.text] = pvalue.text;

       pname.text = ""; 
       pvalue.text = "";

       pname.setFocus();
      }

     ]]>
    </mx:Script>

    <mx:VBox>
     <mx:Label text="Dictionary Contents" />
     <mx:TextArea id="txt" width="350" height="200" />
     <mx:HBox>
      <mx:VBox>
       <mx:Label text="New Property Name" />
       <mx:TextInput id="pname" text="pets" />
      </mx:VBox>
      <mx:VBox>
       <mx:Label text="New Property Value" />
       <mx:TextInput id="pvalue" text="dog, cat, fish" />
      </mx:VBox>
     </mx:HBox>
     <mx:Button id="btn" label="Add" click="onBtnClick()" />
    </mx:VBox>

    <utils:Dictionary id="d">
     <utils:fname>Chris</utils:fname>
     <utils:lname>Nunciato</utils:lname>
    </utils:Dictionary>

</mx:Application>

Hope that helps! If I've totally missed the point, post back and I'll see if I can help out.

Christian Nunciato
Thanks, turns out this was a total rookie mistake where we simply didn't realize that Dictionary isn't in the mx namespace!
macke
+1  A: 

It is possible, if you import the right namespace

    <mx:Application ... xmlns:utils="flash.utils.*">
        <utils:Dictionary>
            <utils:foo>bar</utils:foo>
            ...
        </utils:Dictionary>
        ...

But I see no good reason to do this. The advantage of a Dictionary over a plain Object (or <mx:Model>), what you would normally use, is that you can use objects other than String as key. But you can't write a non-String key in MXML.

In other words: yes you can create one, but there's no point.

Wouter Coekaerts
I agree, except for one situation. We often create mock data using MXML because the format lends itself so well to do this in an efficient manner. It's simply much easier to read and write mock value objects or other types of mocks in MXML than AS. But yeah, I agree with your sentiment otherwise.
macke
+1 for it makes no point. Dict's are great for non-string keys. Otherwise, use an Object.
Marty Pitt