cocktail-trait-configurable
CocktailJS Trait Extension
AA trait to add configure(options)
method that will call setters on each options key.
Install
npm install cocktail --savenpm install cocktail-trait-configurable --save
Trait requires (glue code)
None
Usage
MyClass.js
var cocktail = Configurable = ; cocktail;
index.js
var MyClass = options obj obj2; options = property1: 'value from options' property2: false discarded: 'this should be discarded!'); obj = ; //use the configure method publiclyobj; console; //'value from options'console; //false //or in the constructorobj2 = options; console; //'value from options'console; //false
Configuring the Configurable Trait
Since version 1.1.0
we can use Configurable.withOptions to return a configured Configurable Trait.
This returns a trait that will look into the given options for the setter name given a key. If the key is not found, then it will default to set{Key} method.
Example:
The class will use addItems
instead of setItems since we want to add items to our items properties and will allow to pass an array or a single element.
MyClass.js
var cocktail = Configurable = ; cocktail;
index.js
var MyClass = options obj obj2; options = prop: 'value from options' discarded: 'this should be discarded!' items: 1234); obj = ; //use the configure method publiclyobj; console; //'value from options'console; // [1,2,3,4]
API
The following methods will be publicly available on the host class:
configure(options)
: calls every available setter per each key defined in the the options object.- options: {Object} the object with values that are going to be applied.