Class com.bumpslide.data.Model

com.bumpslide.data.Map
   +--com.bumpslide.data.Model

Description

This class represents an observable mapping of key value pairs

Primitive Map with additional functionality: 1 - ability to add getters/setter for all mapped values 2 - ability to watch for changes via dispatched events

This class was built to provide state management functionality.

Sample setup in Model Locator:

 	var stateVars:Object = {
 		section: 1,
 		selectedIds: [] 		
   } 
   ModelLocator.state = new Model( stateVars, "myappstate" );
 
 

Changing state using ObjectProxy-like getter/setter interface:

 	function onButtonPress() {
 		// If this changes the state, then events will be broadcast to listeners
 		ModelLocator.state.section = 2;
 	} 
 

Listening to changes in some other class:

   
   function init() {
 		ModelLocator.state.addEventListener( Model.VALUE_CHANGED_EVENT, Delegate.create( this, modelValueChanged ) );
 	}
 
 	function modelValueChanged( e:ModelValueChangedEvent ) {
 		trace( e.key +" has changed from "+e.oldValue +" to "+e.newValue);
   }    
 

Responding to model changes in the view using binding:

 
   // Some view clip...
   function onLoad() {
 		ModelLocator.state.bind( "section", this );
 	}
 
   // when section changes, this setter gets called 
   function set section (n) {
 		trace("Section is now "+n);
		// update view now
   }
 
Copyright (c) 2006, David Knape Released under the open-source MIT license. See LICENSE.txt for full license terms.

Field Index

addEventListener, CHANGE_REQUEST_EVENT, debug, MODEL_CHANGED_EVENT, removeEventListener, VALUE_CHANGED_EVENT

Method Index

new Model()

bind(), change(), createAccessors(), getCurrent(), put(), registerBinding(), unbind(), unbindAll(), unregisterBinding()

Inherited from Map

clear(), containsKey(), containsValue(), get(), getKey(), getKeys(), getValue(), getValues(), isEmpty(), remove(), size(), toObject()

Constructor Detail

Model

function Model(source, modelName:String)

Creates a new observable model (object proxy) from an anonymous source object

The model will be created using all enumerable properties in the source object. That is, we are using a for ... in loop on the source object, so new instances of methodless value objects may not work here. If you want to use a specific class as the source object, then be sure to set all properties with default values in the constructor of that class to make sure that all of those properties become enumerable. Otherwise, just use an anonymous object (curly braces) as the source.

see Flash help regarding "Object.isPropertyEnumerable" and "for..in statement" for more info on property enumerability.

The second parameter is simply a string-based name for this model. It is optional.

Parameters

sourceobject
modelNamefor reference in debug statements

Field Detail

debug

public debug:Boolean

VALUE_CHANGED_EVENT

static VALUE_CHANGED_EVENT:String

MODEL_CHANGED_EVENT

static MODEL_CHANGED_EVENT:String

CHANGE_REQUEST_EVENT

static CHANGE_REQUEST_EVENT:String

addEventListener

public addEventListener:Function

removeEventListener

public removeEventListener:Function

Method Detail

createAccessors

public function createAccessors()

Adds getters and setters to the model for every key

put

public function put(key, newValue)

Maps the given key to the value.

null and undefined values are allowed.

Parameters

keythe key used as identifier for the value

Return

the value that was originally mapped to the key or undefined

Overrides

put() in com.bumpslide.data.Map

change

public function change(key, newValue)

Force a change to a state value bypassing the pre-change filters this is needed to allow pre-change filter handlers to change values without triggering an infinite loop

Parameters

key
newValue

getCurrent

public function getCurrent(Void):Object

bind

public function bind(propName:String, target:Object, targetPropOrFunc):ModelBinding

Setup a one-way binding to the model property with the key propName

Binding will be executed whenever that property changes.

Third parameter is optional. By default, a setter in the target will be set with the new property value when a change occurs. The third parameter can be a string or a function. If it is a string, then a setter with that name will be called instead of one with the name of the property we are watching. If the third argument is a function, then that function will be called is the scope of the target object.

Parameters

propName
target
targetPropOrFunc

Return

unbind

public function unbind(propName:String, target:Object)

Unbind a specific property and target

Parameters

propName
target

unbindAll

public function unbindAll(target:Object)

Unbinds all bindings from this model If a target object is passed in as an argument, only bindings tied to that object will be removed

Parameters

target

registerBinding

public function registerBinding(b:ModelBinding)

Binding registration interface (init)

Used by the ModelBinding class to register bindings with the master model. This allows bindings to be unregistered from the model.

Parameters

b

unregisterBinding

public function unregisterBinding(b:ModelBinding):Boolean

Binding registration interface (destroy)

Used by the ModelBinding class to unregister itself if needed.

Parameters

b

Return