Model
A Model
is used to store shared data through out the application
Inheritance: Model
> Dispatch
> Object
Table of Contents #
initialize( [ values : Object ] ) : Model #
This constructor can be called without arguments. An object is optional to set values. The values are copied using a shallow copy.
set( key : String|Object [, value : Object] ) : void #
If the first argument is ob type Object it sets every value of the object as a key value pair.
If key is a String and value is of any type it sets this pair.
set
dispatches the appropriate change-events for every value that was preexisting and changed.
function onNameChanged( value ) { ... } var model = new Model(); model.addListener( "name", onNameChanged ); model.set( "name", "Mike" ); // onNameChanged will be called with the new value. model.set( { name: "Mathias", age: "23" } );
get( [ key : String [, closure : Function] ] ) : Object #
When called with no parameters it returns all values as an Object.
When called with one parameter it will return the value found for this value.
When called with key and closure Function it will return the value found and also set the supplied function as listener. This function will be called immediatly and for every time the value is changed.
var model = new Model(); model.set( "name", "Mike" ); model.set( "age", 34 ); model.get( "name" ); // returns Mike function onAgeChange( value ) { ... } model.get( "age", onAgeChange ); returns 34 // onAgeChange called first time. model.set( "age", 45 ); // onAgeChange called again
change( key : String, closure : Function ) : void #
Sets a callback for a value. The difference to get()
is that it won't call the closure with the current value.
var model = new Model(); model.set( "name", "Mathias" ); function onNameChange( value ) { ... } model.change( "name", onNameChanged ); model.set( "name", "Hooray" ); //onNameChange called once.
change
is a syntactic sugar for addListener
.
model.change( "name", onNameChange ); // is the same as model.addListener( "name", onNameChange );
Documentation generated by mdoc.