4 Replies Latest reply on Nov 26, 2008 9:02 AM by rtalton

    Having trouble with binding data to function

    spacehog Level 1
      I have a repeater with a button inside it. The repeater uses an XML file as its data provider. The repeater seems to be able to the 'name' node from the XML file and populate the 'label' of the button correctly. However, when I make a call to a function from the click event using a databound value the value isn't passed. If I hard code a value into the function call everything works fine. Here is the code:

      <!-- The XML file attributes.xml-->
      <?xml version="1.0" encoding="utf-8"?>
      <attributes>
      <sc>
      <name>MCAE</name>
      <scid>1</scid>
      </sc>
      <sc>
      <name>ECAE</name>
      <scid>2</scid>
      </sc>
      <sc>
      <name>SSCAE</name>
      <scid>3</scid>
      </sc>
      </attributes>

      <!-- Here is the function being called -->

      public function changeTitle(cat:int):void{
      switch (cat){
      case 1:
      appTitle.text="MCAE Monthly Usage";
      lblToolDrop.text="MCAE Tools";
      break;
      case 2:
      appTitle.text="ECAE Monthly Usage";
      break;
      case 3:
      appTitle.text="SSCAE Monthly Usage";
      lblToolDrop.text="SSCAE Tools";
      break;
      default:
      appTitle.text="Unk CAE Usage Center";
      break;
      }

      }

      <!-- Here is the Repeater -->
      <mx:HTTPService id="results" url="model/attributes.xml" resultFormat="e4x" />
      <mx:XMLListCollection id="sclist" source="{results.lastResult.sc}" />
      <mx:VBox>
      <mx:Repeater id="rMMR" dataProvider="{sclist}" >
      <mx:Button id="btnSC" width="150" label="{rMMR.currentItem.name}"
      click="Application.application.currentState='step2';
      Application.application.changeTitle('{rMMR.currentItem.scid}');"/>
      </mx:Repeater>
      </mx:VBox>

      Running the code as is returns the values of the default case. If I remove the single quotes from around the databound {rMMR.currentItem.scid} I get two error messages:

      1. expecting colon before dot
      2. expecting identifier before rightbrace

      When I run the debugger I get this message:
      "warning: unable to bind to property 'sc' on class 'XML' (class is not an IEventDispatcher)"

      The warning makes it seems like my databind is not happening. But if that is the case then how come the name node in the XML file is being bound correctly to the label attribute of the buttons?

      Any suggestions are greatly appreciated.

        • 1. Having trouble with binding data to function
          rtalton Level 4
          It looks like you are passing a string into a function which expects an integer. Since the XML data are just strings, change your function to accept a string.
          Put a break point in the function, go into debug and click the button. Step through the function to see if you are comparing apples to apples instead of an integer to a string.
          • 2. Re: Having trouble with binding data to function
            spacehog Level 1
            I agree, when I call the function:

            Application.application.changeTitle('{rMMR.currentItem.scid}');

            putting the single quotes around the bound value signafies that I am passing a string. The problem is that when I change the call to:

            Application.application.changeTitle({rMMR.currentItem.scid});

            I get the error messages I mentioned in my original post. I even went as far as to change the switch/case statement to be triggered by strings instead of an int (I passed the name node from the XML file instead of the scid node) and the exact same results happened.

            According to debugging when I run the code as:

            Application.application.changeTitle('{rMMR.currentItem.scid}'); the literal string being passed is {rMMR.surrentItem.scid} instead of its value. This tells me that my binding is having issues, but I can't figure out why.
            • 3. Re: Having trouble with binding data to function
              Level 7

              "spacehog" <webforumsuser@macromedia.com> wrote in message
              news:ggjlqq$n1k$1@forums.macromedia.com...
              >I agree, when I call the function:
              >
              > Application.application.changeTitle('{rMMR.currentItem.scid}');
              >
              > putting the single quotes around the bound value signafies that I am
              > passing a
              > string. The problem is that when I change the call to:
              >
              > Application.application.changeTitle({rMMR.currentItem.scid});
              >
              > I get the error messages I mentioned in my original post. I even went as
              > far
              > as to change the switch/case statement to be triggered by strings instead
              > of an
              > int (I passed the name node from the XML file instead of the scid node)
              > and the
              > exact same results happened.
              >
              > According to debugging when I run the code as:
              >
              > Application.application.changeTitle('{rMMR.currentItem.scid}'); the
              > literal
              > string being passed is {rMMR.surrentItem.scid} instead of its value. This
              > tells me that my binding is having issues, but I can't figure out why.

              http://www.magnoliamultimedia.com/flex_examples/Amys_Flex_FAQ.pdf

              Check out Q3


              • 4. Having trouble with binding data to function
                rtalton Level 4
                I don't think you can pass a string to a function and have it evaluated as an AS expression (the curly braces}. I've run into a similar problem like this with no easy solution. I ended up creating a new object (in my case, a textArea), assigning values to it, and dispatching a click event for it. In the event handler, I could then use the object's properties directly.

                In you case, are you simply trying to change the html page's title property? Could you put the titles in your repeater's dataProvider and access them through the currentTarget property in the click event handler?