4 Replies Latest reply on Jun 26, 2011 9:31 AM by bre_an

    EventDispatcher - target question

    bre_an Level 1

      Hi,

       

      I have some objects that change over time (that update comes from a server) and some progress bars for these objects.

      I want to keep the gui stuff seperated from the data model classes, so my object inherites EventDispatcher.

      How do I connect my object instance with the progress bar?

      see also the comments in the code below:

       

      public class MyBar {

          function MyBar(obj:MyObject) {

              obj.addEventListener("change", this.onChange)

          }

       

          function onChange(e:MyEvent) {

              //How do I make sure this is the right Bar and not all bars are updated?

              this.value = e.(target as MyObject).value

          }

      }

       

      public class MyObject extends EventDispatcher() {

          public var value:int

          //called from server

          function serverUpdate(value:int):void {

              //dispatch "change" - but how do I determine which Bar should be changed?

              this.value = value

              this.dispatchEvent(new MyEvent("change", this))

          }

      }

       

      public class MyEvent extends Event {

           public MyEvent(type:String, target:MyObject) {

               this.type = type

               this.target = target

           }

      }

        • 1. Re: EventDispatcher - target question
          pauland Level 4

          I'm not quite sure what problem you're trying to solve.

           

          The bars are passed the oibject they should listen to and they set up listeners that will cause the bars to change when the object dispatches a change event.

           

          Only bars that have been passed the reference to the object will respond to the change, so which bars change in response to objects changing value is entirely explicit. So, the progress bar is connected to the object by this line:

           

          var progressBar:MyBar = new Mybar(obj); // the constructor takes an object parameter.

           

          Am I missing something?

           

          Paul

          1 person found this helpful
          • 2. Re: EventDispatcher - target question
            bre_an Level 1

            I got more than one object and more than one bar.

            Each object should have its own bar.

            Now, if new data from server arrives, I have to update the specific bar for this object, but the object does not know which bar it has to update, when dispatchEvent is called all bars will be updated with the value of one Object.

            I could of course set the bar as variable in the object and do this without the whole event-stuff, but i want to seperate my data model from the gui.

            [Edit]

            or... is there some "magic" so that only the bar that has the MyObject instance will be updated?

             

            Message was edited by: bre_an

            • 3. Re: EventDispatcher - target question
              pauland Level 4

              The object should never know which bar needs to be updated - it doesn't need to.

               

              All the object does is say that it's changed and any bars that are listening for that will change.

               

              So:

               

              var progressBar:MyBar = new Mybar(obj); // the constructor takes an object parameter.

               

              var progressBar2:MyBar = new Mybar(obj2); // the constructor takes an object parameter.

               

              obj changes are seen only by progressBar, and obj2 chnges are only seen by progressBar2.

               

              It works in the way that you want.

               

              Paul

              • 4. Re: EventDispatcher - target question
                bre_an Level 1

                AWESOME! It is working!

                 

                THANK YOU VERY MUCH, pauland!

                 

                There really is some "magic" in the background, I do not have to care about the objects myself, EventDispatcher does this on his own, like paulland wrote!

                 

                I don't even have to create my own Event-Class, because target is set automatically, so

                dispatchEvent(new Event("change")) is all I need to do in MyObject.