8 Replies Latest reply: Jul 11, 2012 5:38 PM by Flex harUI RSS

    Cant understand why binding not works

    sadensmol Community Member

      Hello. Please help me.  simple example not works for me (((

       

      <?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.events.FlexEvent;

       

       

       

                                    private var eee:test123;

                                    protected function application1_creationCompleteHandler(event:FlexEvent):void {

                                              eee = new test123(this);

                                    }

       

                                    protected function button1_clickHandler(event:MouseEvent):void {

                                              dispatchEvent( new Event("test2"));

                                    }

       

                          ]]>

      </fx:Script>

       

      <s:layout>

        <s:VerticalLayout/>

        </s:layout>

       

                <s:Label text="{eee.text}"/>

                <s:Button click="button1_clickHandler(event)"/>

      </s:Application>

       

       

      and file test123.as

       

      package {

                import flash.events.Event;

                import flash.events.EventDispatcher;

       

                import mx.binding.utils.BindingUtils;

       

       

                public class test123 extends EventDispatcher{

                [Bindable(event = "test1")]

                public var text:String ;

       

                          public function test123(obj:EventDispatcher){

                                    obj.addEventListener("test2",ontest2);

                          }

       

                          protected function ontest2(event:Event):void {

                                    text = "fdsfadsf";

                                    dispatchEvent(new Event("test1"));

                          }

                }

      }

       

       

      When i clicked the button label not changes. But if i transfer all code from test123 directly to main program it works fine. What is wrong???

        • 1. Re: Cant understand why binding not works
          Flex harUI Adobe Employee

          I don’t see metadata in the example that is not working.

          • 2. Re: Cant understand why binding not works
            sadensmol Community Member

            Matadata is on own place

             

              [Bindable(event = "test1")]

                      public var text:String ;

            • 3. Re: Cant understand why binding not works
              Flex harUI Adobe Employee

              Aren’t you getting warnings in the console about eee not being bindable?  I would think it would need metadata too.

              • 4. Re: Cant understand why binding not works
                sadensmol Community Member

                Oh. Yes!

                Now it worked!

                Thanks. Warning is here also.

                 

                Question is closed.

                • 5. Re: Cant understand why binding not works
                  sadensmol Community Member

                  Also have a look please. Ive changed test123 class to something like singletone.

                   

                  And it working without external binding. Why???

                   

                  Main class:

                   

                  <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.events.FlexEvent;

                   

                   

                                                protected function application1_creationCompleteHandler(event:FlexEvent):void {

                                                }

                   

                                                protected function button1_clickHandler(event:MouseEvent):void {

                                                          dispatchEvent( new Event("test2"));

                                                }

                   

                                      ]]>

                  </fx:Script>

                   

                  <s:layout>

                    <s:VerticalLayout/>

                    </s:layout>

                   

                            <s:Label text="{test123.getInstance(this).lalala()}"/>

                            <s:Button click="button1_clickHandler(event)"/>

                  </s:Application>

                   

                   

                  test123 class:

                   

                  package {

                            import flash.events.Event;

                            import flash.events.EventDispatcher;

                   

                            import mx.binding.utils.BindingUtils;

                   

                   

                            public class test123 extends EventDispatcher{

                            private var text:String ;

                   

                            private static var instance:test123 = null;

                   

                            public static function getInstance(obj:EventDispatcher):test123{

                                      if (!instance) instance = new test123(obj);

                                      return instance

                            }

                                      public function test123(obj:EventDispatcher){

                                                obj.addEventListener("test2",ontest2);

                                      }

                   

                            [Bindable(event = "test1")]

                            public function lalala():String {

                                                return text;

                            }

                   

                                      protected function ontest2(event:Event):void {

                                                text = "fdsfadsf";

                                                dispatchEvent(new Event("test1"));

                                      }

                            }

                  }

                   

                  Why it working now?

                   

                  The main problem - Ive the same code with singletone in my live application. And sometimes lalala is not fired update. And i dont know why???

                  • 6. Re: Cant understand why binding not works
                    Flex harUI Adobe Employee

                    If you don’t have the binding events on each level of the expression, then you are relying on timing, or that certain parts of the expression do not change.  In your first case, eee was being assigned late, in this case the singleton is ready right away.

                    • 7. Re: Cant understand why binding not works
                      sadensmol Community Member

                      Thanks for explanations. Seems good for me.

                       

                      I started with this investigation once faced binding issue in my code. Can you have a look?

                       

                      1 Ive callback from server

                       

                                                    public function onUserStateChanged(userId:Number,state:int) :void{

                                                              Cc.log(" *** onUserStateChanged "+userId+","+state);

                                                              CommonModel.getInstance().messageTarget.dispatchEvent(new DynamicEvent(Events.USER_STATE_CHANGED, false, false, [userId,state]));

                                                    }

                       

                      2 This messages is accepted in my signletone

                       

                      addEventListener("refreshUserState",onTest);

                       

                      CommonModel.getInstance().messageTarget.addEventListener(Events.USER_STATE_CHANGED,onUserS tateChanged);

                       

                      and then in code:

                       

                                          public function onUserStateChanged(event:DynamicEvent):void{

                                                    var userId:Number = event.eventData[0];

                                                    var state:Number = event.eventData[1];

                       

                                                    var user:Object = findUserById(usersInCurrentConference,userId);

                                                    if (!user){

                                                              Cc.log(" !!! User not found while on user state changing for user "+userId+" !!!");

                        return;

                                                    }

                       

                                                              user.state = state;

                                                              dispatchEvent(new Event("refreshUserState"));

                                          }

                       

                      In this class i ve also two additional methods:

                       

                       

                                          private function onTest(event:Event):void{

                        trace ("fasdfasdf");

                                          }

                       

                      and

                       

                       

                                          [Bindable (event = "refreshUserState")]

                                          public function isUserDisconnected(user:Object):Boolean{

                                                    if (!user) return false;

                                                    return user.state == User.STATUS_DISCONNECTED;

                                          }

                       

                      3 From external code ( list renderer ) which created after first access to singletone

                       

                                <s:BorderContainer id="disconnectedMessageContainer" left="1" top="1" right="1" bottom="1" visible="{UserHelper.getInstance().isUserDisconnected(data)}" backgroundAlpha="0.6" backgroundColor="0x000000"

                                                                           mouseChildren="false">

                                          <s:HGroup width="100%" height="100%" horizontalAlign="center" verticalAlign="middle">

                                                    <s:Label color="0xffffff" text = "DISCONNECTED" fontWeight="bold"/>

                        </s:HGroup>

                      </s:BorderContainer>

                       

                       

                      And what i see. When calback comes onTest function fires with no problems. also in debug i see that  refreshUserState fires also but disconnectedMessageContainer not refreshes. i can see in debug any requat to isUserDisconnected function.

                       

                      What does it mean? Can you help and advise?

                      • 8. Re: Cant understand why binding not works
                        Flex harUI Adobe Employee

                        I’m unclear what code ran and didn’t run.  I would put in more breakpoints or trace statements.