Skip to content

Instantly share code, notes, and snippets.

@robertpenner
Created December 3, 2009 05:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertpenner/247920 to your computer and use it in GitHub Desktop.
Save robertpenner/247920 to your computer and use it in GitHub Desktop.
A simple example of using Signals to dispatch changes to a property.
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