Created
December 3, 2009 05:42
-
-
Save robertpenner/247920 to your computer and use it in GitHub Desktop.
A simple example of using Signals to dispatch changes to a property.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import org.osflash.signals.Signal; | |
import flash.display.Sprite; | |
public class SignalsHelloWorld extends Sprite | |
{ | |
private var radio:Radio; | |
private var messageListener:MessageListener; | |
public function SignalsHelloWorld() | |
{ | |
radio = new Radio(); | |
messageListener = new MessageListener(); | |
// Hook the listener up to the Radio's Signal. | |
radio.messageChanged.add(messageListener.onMessageChanged); | |
// Change the message, dispatching it to listeners. | |
radio.message = 'Hello World'; | |
} | |
} | |
} | |
////////// PRIVATE CLASSES ////////// | |
import org.osflash.signals.Signal; | |
class Radio | |
{ | |
// This is the public API to which listeners subscribe. | |
public var messageChanged:Signal; | |
private var _message:String; | |
public function Radio() | |
{ | |
// Create a Signal that dispatches a String. | |
messageChanged = new Signal(String); | |
} | |
public function get message():String { return _message; } | |
// The setter uses the Signal to dispatch the changed value. | |
public function set message(value:String):void | |
{ | |
_message = value; | |
messageChanged.dispatch(value); | |
} | |
} | |
class MessageListener | |
{ | |
// Receive the value dispatched from the Signal. | |
// This is like an event handler but without the Event wrapper. | |
public function onMessageChanged(message:String):void | |
{ | |
trace('heard message: ' + message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment