5 Replies Latest reply on Sep 12, 2007 4:13 PM by Senor_Roberto

    Problem with binding and renderer

    I am Maya Level 1
      I have the following simple application with just a List component and a HTTPService on it:

      <!-- Simple.mxml -->
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="usersSrv.send(null);">
      <mx:List id="myList" dataProvider="{usersSrv.lastResult.users.user}" itemRenderer="MyRenderer" width="156"></mx:List>
      <mx:HTTPService id="usersSrv" url=" http://www.domain.com/GetUsers.asp" showBusyCursor="true" />
      </mx:Application>

      The list elements are rendered with the component MyRenderer:

      <!-- MyRenderer.mxml -->
      <?xml version="1.0" encoding="utf-8"?>
      <mx:CheckBox xmlns:mx=" http://www.adobe.com/2006/mxml" label="{data.userName}" selected="{data.isAssigned}"></mx:CheckBox>

      When i run the application it looks fine! The list is filled with names and the correct checkboxes are checked!

      There is just this problem with the console! It shows the following error message for each user shown:

      warning: unable to bind to property 'userName' on class 'Object' (class is not an IEventDispatcher)
      warning: unable to bind to property 'isAssigned' on class 'Object' (class is not an IEventDispatcher)


      WHATS WRONG WITH MY CODE?

      I'VE KEPT IT SIMPLE, SO WHAT DID I MISS?

      I WOULD LIKE THE ELEMENTS TO BIND BUT I GUESS THEY WON'T UNTIL I GET RID OF THIS ERROR MESSAGE!
        • 1. Re: Problem with binding and renderer
          theLoggerGuy Level 1
          Good question. At last count I have about 30 of these annoying messages in my console. I have been ignoring them for months using the theory of, it's only a warning!! I can't wait to read the comments from the wise person who replies to this post with the answer.
          • 2. Re: Problem with binding and renderer
            Senor_Roberto Level 1
            The reason you're getting these warnings is that the HTTP service is responding with anonymous (runtime) objects in the result, but your MXML bindings are referencing specific field names. The compiler doesn't know whether userName or isAssigned will actually exist at compile time, that's why it throws the warning. Welcome to static typing!

            It all works fine because at runtime you send back the correct object and the properties match up.

            If you change the resultFormat of the HTTP service to E4X and instead use E4X notation to bind the property, it doesn't throw the warnings and you should get the same result.
            • 3. Re: Problem with binding and renderer
              I am Maya Level 1
              Thanks Senor Roberto! That sounds simple! I didn't know about E4X because i've only used Flex for a week. I'll get back to you with the final solution(today or tomorrow)!
              • 4. Re: Problem with binding and renderer
                I am Maya Level 1
                Okey, now I've changed the resultFormat to E4X and got it almost up and running again, because i still get the console error message and the checkbox selected value is not binding correctly (they both set to true).

                - I've tried moving the values into attributes instead of tag content, but this made no difference.
                - I've tried using the "selectedField" attribute on the checkbox to point out that "isAssigned" is the value, but this didn't work.
                - I've tried switching the values of userName and isAssigned. This made the correct values for "isAssigned" to show up as the checkbox label. So it seem like this value binds correctly to the "label" attribute but not to the "selected" attribute. Is there anything about binding boolean values I need to know?
                - I believe I've done everything to get rid of the error message using E4X, but it's still there and now it says "'user' on class'XML'". I don't understand a thing!

                I hope the complete example below help you to help me! Thanks for the help so far!


                Here is my XML:
                <?xml version="1.0" encoding="UTF-8"?>
                <users>
                <user userName="Jane" isAssigned="false" />
                <user userName="Erik" isAssigned="true" />
                </users>

                Here is my application:
                <?xml version="1.0" encoding="utf-8"?>
                <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="usersSrv.send(null);">
                <mx:List id="myList" dataProvider="{usersSrv.lastResult.user}" itemRenderer="MyRenderer" width="156"></mx:List>
                <mx:HTTPService id="usersSrv" url=" http://www.domain.com/test.xml" showBusyCursor="true" resultFormat="e4x"/>
                </mx:Application>

                Here is my renderer:
                <!-- MyRenderer.mxml -->
                <?xml version="1.0" encoding="utf-8"?>
                <mx:CheckBox xmlns:mx=" http://www.adobe.com/2006/mxml" label="{(data as XML).@userName}" selected="{(data as XML).@isAssigned}">
                </mx:CheckBox>

                Here is the console error message:
                warning: unable to bind to property 'userName' on class 'MyRenderer'
                warning: unable to bind to property 'isAssigned' on class 'MyRenderer'
                warning: unable to bind to property 'user' on class 'XML' (class is not an IEventDispatcher)
                warning: unable to bind to property 'userName' on class 'MyRenderer'
                warning: unable to bind to property 'isAssigned' on class 'MyRenderer'
                warning: unable to bind to property 'userName' on class 'MyRenderer'
                warning: unable to bind to property 'isAssigned' on class 'MyRenderer'
                • 5. Re: Problem with binding and renderer
                  Senor_Roberto Level 1
                  I knew I should have tested it :) It looks like my E4X trick didn't work.

                  I actually built your app with your code (copy and paste) and I didn't get the warnings you're getting and my compiler is set to strict. I usually do get those errors though when something isn't set as [Bindable].

                  Because you're not using a custom component and don't have an associated AS file to bind properties directly, try this (before the list tag in the application MXML):

                  <mx:Metadata>[Bindable]</mx:Metadata>

                  This makes all properties of the application class bindable.

                  Let me know how you go.
                  Cheers