Packagecom.soma.core.wire
Classpublic class Wire
InheritanceWire Inheritance Object
Implements IWire, flash.events.IEventDispatcher
Subclasses Mediator

Author: Romuald Quantin - www.soundstep.com

Resources: http://www.soundstep.com/downloads/somacore

Actionscript version: 3.0

Copyright: Mozilla Public License 1.1 (MPL 1.1) http://www.opensource.org/licenses/mozilla1.1.php

A Wire is a class that will hold the logic of the Application.

Wires can be used in many ways, depending on how you want to manage your views, commands and models. A wire can be used as a manager and handle many models, views or other wires. A wire can also be used in a one-to-one way (as a proxy), a single wire that handles a single view, a single wire that handles a single model, and so on.

Wires can be flexible or rigid depending on how your build your application.

A wire has access to everything in the framework: you can create views, add and dispatch commands, create models, access to the framework instance, access to the stage, and so on.

A wire can also be in control of the commands that are dispatched by listening to them and even stop their execution if needed (see the examples in this page).

View the examples

See also

com.soma.core.wire.SomaWires


Public Properties
 PropertyDefined By
  injector : ISomaInjector
[read-only]
Wire
  instance : ISoma
Retrieves the instance of the framework.
Wire
  mediators : SomaMediators
[read-only]
Wire
  stage : Stage
[read-only] Get the stage that has been registered to the framework.
Wire
Protected Properties
 PropertyDefined By
  _name : String
Name of the wire.
Wire
Public Methods
 MethodDefined By
  
Wire(name:String = null)
Create an instance of a Wire class.
Wire
  
addCommand(commandName:String, command:Class):void
Registers a command to the framework.
Wire
  
addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
Registers an event listener object with an EventDispatcher object so that the listener receives notification of an event.
Wire
  
addModel(modelName:String, model:IModel):IModel
Registers a model to the framework.
Wire
  
addView(viewName:String, view:Object):Object
Registers a view to the framework.
Wire
  
addWire(wireName:String, wire:IWire):IWire
Registers a wire to the framework.
Wire
  
dispatchEvent(event:Event):Boolean
Dispatches an event into the event flow.
Wire
  
dispose():void
Method that can you can override, called when the wire has been removed from the framework.
Wire
  
getCommand(commandName:String):Class
Retrieves the command class that has been registered with a command name.
Wire
  
getCommands():Array
Retrieves all the command names (event type) that have been registered to the framework.
Wire
  
Retrieves the last sequence command that has been instantiated in the framework.
Wire
  
getModel(modelName:String):IModel
Retrieves the model instance that has been registered using its name.
Wire
  
getModels():Dictionary
Retrieves all the model instances that have been registered to the framework.
Wire
  
getName():String
Retrieves the name of the wire.
Wire
  
Retrieves all the sequence command instances that are running.
Wire
  
Retrieves the sequence command instance using an event instance that has been created from this sequence command.
Wire
  
getView(viewName:String):Object
Retrieves the view instance that has been registered using its name.
Wire
  
getViews():Dictionary
Retrieves all the view instances that have been registered to the framework.
Wire
  
getWire(wireName:String):IWire
Retrieves the wire instance that has been registered using its name.
Wire
  
getWires():Dictionary
Retrieves all the wire instances that have been registered to the framework.
Wire
  
hasCommand(commandName:String):Boolean
Indicates wether a command has been registered to the framework.
Wire
  
hasEventListener(type:String):Boolean
Checks whether the EventDispatcher object has any listeners registered for a specific type of event.
Wire
  
hasModel(modelName:String):Boolean
Indicates wether a model has been registered to the framework.
Wire
  
hasView(viewName:String):Boolean
Indicates wether a view has been registered to the framework.
Wire
  
hasWire(wireName:String):Boolean
Indicates wether a wire has been registered to the framework.
Wire
  
initialize():void
Method that can you can override, called when the wire has been registered to the framework.
Wire
  
isPartOfASequence(event:Event):Boolean
Indicates wether an event has been instantiated from a ISequenceCommand class.
Wire
  
Wire
  
removeCommand(commandName:String):void
Removes a command from the framework.
Wire
  
removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
Removes a listener from the EventDispatcher object.
Wire
  
removeModel(modelName:String):void
Removes a model from the framework and call the dispose method of this model.
Wire
  
removeView(viewName:String):void
Removes a view from the framework and call the (optional) dispose method of this view.
Wire
  
removeWire(wireName:String):void
Removes a wire from the framework and call the dispose method of this wire.
Wire
  
setName(name:String):void
Sets the name of the wire.
Wire
  
Stops all the sequence command instances that are running.
Wire
  
stopSequencer(sequencer:ISequenceCommand):Boolean
Stops a sequence command using the sequence command instance itself.
Wire
  
stopSequencerWithEvent(event:Event):Boolean
Stops a sequence command using an event instance that has been created from this sequence command.
Wire
  
willTrigger(type:String):Boolean
Checks whether an event listener is registered with this EventDispatcher object or any of its ancestors for the specified event type.
Wire
Property Detail
_nameproperty
protected var _name:String

Name of the wire.

injectorproperty 
injector:ISomaInjector  [read-only]


Implementation
    public function get injector():ISomaInjector
instanceproperty 
instance:ISoma

Retrieves the instance of the framework.


Implementation
    public function get instance():ISoma
    public function set instance(value:ISoma):void

Example
var myExtendedSomaClass:SomaApplication = SomaApplication(instance);
mediatorsproperty 
mediators:SomaMediators  [read-only]


Implementation
    public function get mediators():SomaMediators
stageproperty 
stage:Stage  [read-only]

Get the stage that has been registered to the framework.


Implementation
    public function get stage():Stage
Constructor Detail
Wire()Constructor
public function Wire(name:String = null)

Create an instance of a Wire class. The Wire class should be extended.

Parameters
name:String (default = null) — Name of the wire.
Method Detail
addCommand()method
public final function addCommand(commandName:String, command:Class):void

Registers a command to the framework.

Parameters

commandName:String — Event type that is used as a command name.
 
command:Class — Class that will be executed when a command has been dispatched.

See also


Example
addCommand(MyEvent.DOSOMETHING, MyCommand);
addEventListener()method 
public final function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void

Registers an event listener object with an EventDispatcher object so that the listener receives notification of an event.

Parameters

type:String — The type of event.
 
listener:Function — The listener function that processes the event.
 
useCapture:Boolean (default = false) — Determines whether the listener works in the capture phase or the target and bubbling phases.
 
priority:int (default = 0) — The priority level of the event listener.
 
useWeakReference:Boolean (default = false) — Determines whether the reference to the listener is strong or weak.

addModel()method 
public final function addModel(modelName:String, model:IModel):IModel

Registers a model to the framework.

Parameters

modelName:String — Name of the model.
 
model:IModel — Instance of the model.

Returns
IModel — The model instance.

See also


Example
addModel(MyModel.NAME, new MyModel());
addView()method 
public final function addView(viewName:String, view:Object):Object

Registers a view to the framework.

Parameters

viewName:String — Name of the view.
 
view:Object — Instance of the view.

Returns
Object — The view instance.

See also


Example
addView(MySprite.NAME, new MySprite());
addWire()method 
public final function addWire(wireName:String, wire:IWire):IWire

Registers a wire to the framework.

Parameters

wireName:String — Name of the wire.
 
wire:IWire — Instance of the wire.

Returns
IWire — The wire instance.

See also


Example
addWire(MyWire.NAME, new MyWire());
dispatchEvent()method 
public final function dispatchEvent(event:Event):Boolean

Dispatches an event into the event flow. The event target is the EventDispatcher object upon which dispatchEvent() is called.

Parameters

event:Event — The event object dispatched into the event flow.

Returns
Boolean — A value of true unless preventDefault() is called on the event, in which case it returns false.
dispose()method 
public function dispose():void

Method that can you can override, called when the wire has been removed from the framework.

getCommand()method 
public final function getCommand(commandName:String):Class

Retrieves the command class that has been registered with a command name.

Parameters

commandName:String — Event type that is used as a command name.

Returns
Class — A class.

See also


Example
var commandClass:ICommand = getCommand(MyEvent.DOSOMETHING) as ICommand;
getCommands()method 
public final function getCommands():Array

Retrieves all the command names (event type) that have been registered to the framework.

Returns
Array — An Array of String (command name).

See also


Example
var commands:Array = getCommands();
getLastSequencer()method 
public final function getLastSequencer():ISequenceCommand

Retrieves the last sequence command that has been instantiated in the framework.

Returns
ISequenceCommand — An ISequenceCommand instance.

See also


Example
var lastSequencer:ISequenceCommand = getLastSequencer();
getModel()method 
public final function getModel(modelName:String):IModel

Retrieves the model instance that has been registered using its name.

Parameters

modelName:String — Name of the model.

Returns
IModel — A IModel instance.

See also


Example
var myModel:MyModel = getModel(MyModel.NAME) as MyModel;
getModels()method 
public final function getModels():Dictionary

Retrieves all the model instances that have been registered to the framework.

Returns
Dictionary — A Dictionary (the key of the Dictionary is the name used for the registration).

See also


Example
var models:Dictionary = getModels();
getName()method 
public function getName():String

Retrieves the name of the wire.

Returns
String — A String.
getRunningSequencers()method 
public final function getRunningSequencers():Array

Retrieves all the sequence command instances that are running.

Returns
Array — An Array of ISequenceCommand instances.

See also


Example
var sequencers:Array = getRunningSequencers();
getSequencer()method 
public final function getSequencer(event:Event):ISequenceCommand

Retrieves the sequence command instance using an event instance that has been created from this sequence command.

Parameters

event:Event — Event instance.

Returns
ISequenceCommand — A sequencer (ISequenceCommand).

See also


Example
var sequencer:ISequenceCommand = getSequencer(myEvent);
getView()method 
public final function getView(viewName:String):Object

Retrieves the view instance that has been registered using its name.

Parameters

viewName:String — Name of the view.

Returns
Object — An Object instance.

See also


Example
var mySprite:MySprite = getView(MySprite.NAME) as MySprite;
getViews()method 
public final function getViews():Dictionary

Retrieves all the view instances that have been registered to the framework.

Returns
Dictionary — A Dictionary (the key of the Dictionary is the name used for the registration).

See also


Example
var sprites:Dictionary = getViews();
getWire()method 
public final function getWire(wireName:String):IWire

Retrieves the wire instance that has been registered using its name.

Parameters

wireName:String — Name of the wire.

Returns
IWire — A wire instance.

See also


Example
var myWire:MyWire = getWire(MyWire.NAME) as MyWire;
getWires()method 
public final function getWires():Dictionary

Retrieves all the wire instances that have been registered to the framework.

Returns
Dictionary — A Dictionary (the key of the Dictionary is the name used for the registration).

See also


Example
var wires:Dictionary = getWires();
hasCommand()method 
public final function hasCommand(commandName:String):Boolean

Indicates wether a command has been registered to the framework.

Parameters

commandName:String — Event type that is used as a command name.

Returns
Boolean — A Boolean.

See also


Example
hasCommand(MyEvent.DOSOMETHING);
hasEventListener()method 
public final function hasEventListener(type:String):Boolean

Checks whether the EventDispatcher object has any listeners registered for a specific type of event.

Parameters

type:String — The type of event.

Returns
Boolean — A value of true if a listener of the specified type is registered; false otherwise.
hasModel()method 
public final function hasModel(modelName:String):Boolean

Indicates wether a model has been registered to the framework.

Parameters

modelName:String — Name of the model.

Returns
Boolean — A Boolean.

See also


Example
hasModel(MyModel.NAME);
hasView()method 
public final function hasView(viewName:String):Boolean

Indicates wether a view has been registered to the framework.

Parameters

viewName:String — Name of the view.

Returns
Boolean — A Boolean.

See also


Example
hasView(MySprite.NAME);
hasWire()method 
public final function hasWire(wireName:String):Boolean

Indicates wether a wire has been registered to the framework.

Parameters

wireName:String — Name of the wire.

Returns
Boolean — A Boolean.

See also


Example
hasWire(MyWire.NAME);
initialize()method 
public function initialize():void

Method that can you can override, called when the wire has been registered to the framework.

isPartOfASequence()method 
public final function isPartOfASequence(event:Event):Boolean

Indicates wether an event has been instantiated from a ISequenceCommand class.

Parameters

event:Event

Returns
Boolean — A Boolean.

See also


Example
var inSequence:Boolean = isPartOfASequence(myEvent);
postConstruct()method 
public function postConstruct():void

removeCommand()method 
public final function removeCommand(commandName:String):void

Removes a command from the framework.

Parameters

commandName:String — Event type that is used as a command name.

See also


Example
removeCommand(MyEvent.DOSOMETHING);
removeEventListener()method 
public final function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void

Removes a listener from the EventDispatcher object. If there is no matching listener registered with the EventDispatcher object, a call to this method has no effect.

Parameters

type:String — The type of event.
 
listener:Function — The listener object to remove.
 
useCapture:Boolean (default = false) — Specifies whether the listener was registered for the capture phase or the target and bubbling phases.

removeModel()method 
public final function removeModel(modelName:String):void

Removes a model from the framework and call the dispose method of this model.

Parameters

modelName:String — Name of the model.

See also


Example
removeModel(MyModel.NAME);
removeView()method 
public final function removeView(viewName:String):void

Removes a view from the framework and call the (optional) dispose method of this view.

Parameters

viewName:String — Name of the view.

See also


Example
removeView(MySprite.NAME);
removeWire()method 
public final function removeWire(wireName:String):void

Removes a wire from the framework and call the dispose method of this wire.

Parameters

wireName:String — Name of the wire.

See also


Example
removeWire(MyWire.NAME);
setName()method 
public function setName(name:String):void

Sets the name of the wire.

Parameters

name:String — A String.

stopAllSequencers()method 
public final function stopAllSequencers():void

Stops all the sequence command instances that are running.

See also


Example
stopAllSequencers();
stopSequencer()method 
public final function stopSequencer(sequencer:ISequenceCommand):Boolean

Stops a sequence command using the sequence command instance itself.

Parameters

sequencer:ISequenceCommand — The sequence command instance.

Returns
Boolean — A Boolean (true if the sequence command has been stopped).

See also


Example
var success:Boolean = stopSequencer(mySequenceCommand);
stopSequencerWithEvent()method 
public final function stopSequencerWithEvent(event:Event):Boolean

Stops a sequence command using an event instance that has been created from this sequence command.

Parameters

event:Event — Event instance.

Returns
Boolean — A Boolean (true if a sequence command has been stopped).

See also


Example
var success:Boolean = stopSequencerWithEvent(myEvent);
willTrigger()method 
public final function willTrigger(type:String):Boolean

Checks whether an event listener is registered with this EventDispatcher object or any of its ancestors for the specified event type.

Parameters

type:String — The type of event.

Returns
Boolean — A value of true if a listener of the specified type will be triggered; false otherwise.
Examples
Create a wire
package  {
    import com.soma.core.wire.Wire;
    import com.soma.core.interfaces.IWire;
    
    public class WireExample extends Wire implements IWire {
        
        public static const NAME:String = "Wire example name";
        
        public function WireExample() {
            super(NAME);
        }
        
        override public function initialize():void {
            // called when the wire has been registered to the framework
        }
        
        override public function dispose():void {
            // called when the wire has been removed from the framework
        }
        
    }
}
     
Add a wire.
addWire(WireExample.NAME, new WireExample());
     
Remove a wire.
removeWire(WireExample.NAME);
     
Retrieve a wire.
var wire:WireExample = getWire(WireExample.NAME) as WireExample;
     
Create a shortcut to retrieve a wire is a good practice (not necessary with injection enabled).
private function get wireExample():WireExample {
    return getWire(WireExample.NAME) as WireExample;
}
private function doSomething():void {
    trace(wireExample.myWireProperty);
}
     
Listening to a command in a wire.
override public function initialize():void {
    // called when the wire has been registered to the framework
     addEventListener(MyEvent.DO_SOMETHING, handler);
}
     
Stopping the execution of a command in a wire. You need to set the cancelable property of the event to true when you dispatch it. Any command can be stopped using the flash event built-in method: preventDefault.
override public function initialize():void {
    // called when the wire has been registered to the framework
     addEventListener(MyEvent.DO_SOMETHING, myEventHandler);
}
private function myEventHandler(event:MyEvent):void {
    e.preventDefault();
}