Packagecom.soma.core.model
Classpublic class Model
InheritanceModel Inheritance Object
Implements IModel

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

The model is the class used to manage you application's data model.

The data can be XML, local data, data retrieved from a server or anything. Ideally, the data should be set to the data property of the model instance, but you are free to create specific getters.

View the examples

See also

com.soma.core.model.SomaModels


Public Properties
 PropertyDefined By
  data : Object
Data of the model.
Model
  dispatcher : IEventDispatcher
EventDispatcher instance of the model.
Model
Protected Properties
 PropertyDefined By
  _data : Object
Variable that can be used to hold you data.
Model
  _dispatcher : IEventDispatcher
Instance of a EventDispatcher that can be used to dispatch commands.
Model
  _name : String
Name of the model.
Model
Public Methods
 MethodDefined By
  
Model(name:String = null, data:Object = null, dispatcher:IEventDispatcher = null)
Create an instance of a Model class.
Model
  
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.
Model
  
dispatchEvent(event:Event):Boolean
Dispatches an event into the event flow.
Model
  
dispose():void
Method that can you can override, called when the model has been removed from the framework.
Model
  
getName():String
Retrieves the name of the model.
Model
  
hasEventListener(type:String):Boolean
Checks whether the EventDispatcher object has any listeners registered for a specific type of event.
Model
  
initialize():void
Method that can you can override, called when the model has been registered to the framework.
Model
  
Model
  
removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
Removes a listener from the EventDispatcher object.
Model
  
setName(value:String):void
Sets the name of the model.
Model
  
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.
Model
Property Detail
_dataproperty
protected var _data:Object

Variable that can be used to hold you data.

The default value is null;.

_dispatcherproperty 
protected var _dispatcher:IEventDispatcher

Instance of a EventDispatcher that can be used to dispatch commands.

The default value is Framework instance (Soma instance)..

_nameproperty 
protected var _name:String

Name of the model.

dataproperty 
data:Object

Data of the model.


Implementation
    public function get data():Object
    public function set data(value:Object):void
dispatcherproperty 
dispatcher:IEventDispatcher

EventDispatcher instance of the model.

The default value is The framework instance..


Implementation
    public function get dispatcher():IEventDispatcher
    public function set dispatcher(value:IEventDispatcher):void
Constructor Detail
Model()Constructor
public function Model(name:String = null, data:Object = null, dispatcher:IEventDispatcher = null)

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

Parameters
name:String (default = null) — Name of the model.
 
data:Object (default = null) — Data of the model.
 
dispatcher:IEventDispatcher (default = null) — EventDispatcher instance that can be used to dispatch commands.
Method Detail
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.

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 model has been removed from the framework.

getName()method 
public function getName():String

Retrieves the name of the model.

Returns
String — A String.
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.
initialize()method 
public function initialize():void

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

postConstruct()method 
public function postConstruct():void

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.

setName()method 
public function setName(value:String):void

Sets the name of the model.

Parameters

value:String — A String.

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 model.
package  {
    import com.soma.core.interfaces.IModel;
    import com.soma.core.model.Model;
    
    public class ModelExample extends Model implements IModel {
        
        public static const NAME:String = "Model example name";
        
        public function ModelExample() {
            super(NAME);
        }
        
        override public function initialize():void {
            // called when the model has been registered to the framework
            data = new XML('<myXML/>');
            // you can use the model as a dispatcher (default dispatcher is the framework instance) to dispatch commands, example:
            dispatchEvent(new MyEvent(MyEvent.DATA_READY));
        }
        
        override public function dispose():void {
            // called when the model has been removed from the framework
            data = null;
        }
        
    }
}
     
Add a model.
addModel(ModelExample.NAME, new ModelExample());
     
Remove a model.
removeModel(ModelExample.NAME);
     
Retrieve a model.
var model:ModelExample = getModel(ModelExample.NAME) as ModelExample;
     
Create a shortcut to retrieve a model is a good practice (not necessary with injection enabled).
private function get modelExample():ModelExample {
    return getModel(ModelExample.NAME) as ModelExample;
}
private function doSomething():void {
    trace(modelExample.data);
}