create - PronabM/State.js GitHub Wiki

Create

Objective

Appends property in the existing state (mutates the original state). The is called using two parameters. The first parameter is where to append the state and the second being what property to append.

Implementation

If two parameters are passed the the first parameter is split with "." (dot) and traversed in a sequence, and the property is added to the required position with the given value.

myState.create('range.type',    {
    absolute: true
});

If we do myState.getState() the following output is shown

{
    range: {
        start: 1, 
        end: 5, 
        type: {
            absolute: true 
        }
    },
    visible: true 
}

The same function can be called with only one parameter, just by passing the new state properties. In this case it appends the property in the base.

myState.create({ focus: null });

If we do myState.getState() the following output is shown. In this case we are adding all the properties to the base of the observable i.e. core.

{
    range: {
        start: 1, 
        end: 5, 
        type: {
            absolute: true 
        }
    },
    visible: true, 
    focus: null
}