com.bumpslide.data.Map +--com.bumpslide.data.Model
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.
new Model()
clear(), containsKey(), containsValue(), get(), getKey(), getKeys(), getValue(), getValues(), isEmpty(), remove(), size(), toObject()
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.
source | object |
modelName | for reference in debug statements |
public debug:Booleanstatic VALUE_CHANGED_EVENT:Stringstatic MODEL_CHANGED_EVENT:Stringstatic CHANGE_REQUEST_EVENT:Stringpublic addEventListener:Functionpublic removeEventListener:Functionpublic function createAccessors()
Adds getters and setters to the model for every key
public function put(key, newValue)
Maps the given key to the value.
null and undefined values are allowed.
key | the key used as identifier for the value
|
the value that was originally mapped to the key or undefined
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
key | |
newValue |
public function bind(propName:String, target:Object, targetPropOrFunc):ModelBindingSetup 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.
propName | |
target | |
targetPropOrFunc |
public function unbind(propName:String, target:Object)Unbind a specific property and target
propName | |
target |
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
target |
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.
b |
public function unregisterBinding(b:ModelBinding):BooleanBinding registration interface (destroy)
Used by the ModelBinding class to unregister itself if needed.
b |