ChangeRequest

When a change occurs, ChangeRequests are used by Parcels to describe what to change and how to change it. These ChangeRequests are propagated upward to the top level Parcel.

ChangeRequests contain an array of actions to perform.

ChangeRequests can most often be accessed in handleChange and modifyUp functions. Most of the time these operate invisibly, and it’s rare that you’ll create these yourself.

import ChangeRequest from 'dataparcels/ChangeRequest';
import ChangeRequest from 'react-dataparcels/ChangeRequest';
new ChangeRequest({
   action: Action|Action[] = []
});

Properties

prevData

prevData: Object (ParcelData)

Returns the Parcels data before the change was applied.

nextData

nextData: Object (ParcelData)

Returns the Parcels data after the change was applied.

originId

originId: string

Returns the id of the Parcel that initiated the change request.

originPath

originPath: Array<number|string>

Returns the path of the Parcel that initiated the change request.

actions

actions: Array<Action>

Returns the array of actions that this ChangeRequest contains. These actions are reduced onto prevData to produce nextData.

Methods

hasValueChanged()

hasValueChanged(keyPath: Array<string|number>): boolean

Return a boolean indicating if the value at the given keyPath has changed any of its data as a result of this ChangeRequest.

hasDataChanged()

hasDataChanged(keyPath: Array<string|number>): boolean

Return a boolean indicating if the value or meta at the given keyPath has changed any of its data as a result of this ChangeRequest.

getDataIn()

getDataIn(keyPath: Array<string|number>): Object

A convenience method for returning the previous and next data for a Parcel at the given keyPath.

let parcel = new Parcel({
    handleChange: (parcel, changeRequest) => {
        console.log(changeRequest.getDataIn(['abc', 'def']));
        // ^ the above logs out
        // {
        //     prev: {
        //         value: 123,
        //         meta: {},
        //         ...
        //     },
        //     next: {
        //         value: 456,
        //         meta: {},
        //         ...
        //     }
        // }
    }
    value: {
        abc: {
            def: 123
        }
    }
});

parcel
    .get('abc')
    .get('def')
    .set(456);

// ^ sets abc.def to 456

merge()

merge(other: ChangeRequest): ChangeRequest

Merges the other ChangeRequest onto this ChangeRequest. Actions from other are appended to the current ChangeRequest. As a performance boost, if two subsequent set actions with the same keyPath are merged into each other, the first will be removed.