frontend/laboratory/dsm/TObservableState.js
/**
 * This trait exposes a set of ko.observable values for a given js object according to a specified mapping schema.
 * The observable state contains the observable values and associated meta information for each observable.
 * Additionally, the trait generates properties to get/set the observable value using common js property syntax.
 *
 * Example:
 *
 * var observable = TObservableState({schema:{id:{
            path: "id",
            type: "number",
            editable: false
        }}).newInstance({id:12321});
    // shorthand for observable.state().id.observable()
    observable.id == 12321
 *
 *
 * @param options
 * @constructor
 */
This trait exposes a set of ko.observable values for a given js object according to a specified mapping schema.
The observable state contains the observable values and associated meta information for each observable.
Additionally, the trait generates properties to get/set the observable value using common js property syntax.





@param options
@constructor
This trait exposes a set of ko . observable values for a given js object according to a specified mapping schema . The observable state contains the observable values and associated meta information for each observable . Additionally the trait generates properties to get / set the observable value using common js property syntax . @param options @constructor
function TObservableState(options){
    var _params = (function validate(options){
        if(!options) throw new Error("No options provided");
        if(!options.schema) throw new Error("An observable state requires a non null schema. Was: " + options.schema);
        if(!options.types) throw new Error("An observable state requires a non null type mapping. Was: " + options.types);
        return {
           schema: options.schema,
           types : options.types,
           copyJson : options.copyJson || false
        };
    })(options);
    var json; 
    var state;  
    var TObservable = Trait({
        updateJson: function(){
            for(var property in state.properties){
                state.properties[property].jsonbinding.set(state.observables[property]());
            }
        },
        state: function(){
            return state;
        }
    });
    TObservable.initialize = function(options){
        if(!options.json)throw new Error("No source object defined");
        json = options.json;
        state = parseState(json, _params.schema, _params.types);
        Utils.orb.addProperties(state, this);
    };
    return DSM.traitify(TObservable);
}
function TObservableState ( options ) { var _params = ( function validate ( options ) { if ( ! options ) throw new Error ( "No options provided" ) ; if ( ! options . schema ) throw new Error ( "An observable state requires a non null schema. Was: " + options . schema ) ; if ( ! options . types ) throw new Error ( "An observable state requires a non null type mapping. Was: " + options . types ) ; return { schema : options . schema , types : options . types , copyJson : options . copyJson || false } ; } ) ( options ) ; var json ; var state ; var TObservable = Trait ( { updateJson : function ( ) { for ( var property in state . properties ) { state . properties [ property ] . jsonbinding . set ( state . observables [ property ] ( ) ) ; } } , state : function ( ) { return state ; } } ) ; TObservable . initialize = function ( options ) { if ( ! options . json ) throw new Error ( "No source object defined" ) ; json = options . json ; state = parseState ( json , _params . schema , _params . types ) ; Utils . orb . addProperties ( state , this ) ; } ; return DSM . traitify ( TObservable ) ; }
This trait exposes a set of ko.observable values for a given js object according to a specified mapping schema.
This trait exposes a set of ko . observable values for a given js object according to a specified mapping schema .
// validate parameters and configure with defaults; // PRIVATE INSTANCE STATE // the json underlying the observable state // the observable state as constructed by the initializer (json + schema) // PUBLIC