Managing a Custom Widget State

Enabling GeneWidgetStateAware allows you to save and restore some state properties of a widget. For more details, please refer to Section Understanding Widget State.

GeneBaseDataWidgetComponent provides a base implementation of the Widget state management, and when using this class as the base class of a Widget the only methods that need to be overridden are:

restoreWidgetState(state: T);

getDefaultWidgetState(): T;

The provided Sample Map Widget illustrates how to use this API to save and restore the map position and zoom level.

/**
     * Method that is being called when Platform Framework injects a widget state.
     *
     * This method restore the Map center from the saved stated.
     * @param widgetState
     */
    applyWidgetState(widgetState:SampleMapState) {
        if(widgetState) {
            this.center = {lat: widgetState.lat, lng: widgetState.lng};
            this.zoom = widgetState.zoom;
        }
    }

    /**
     * When the Map center or zoom change, this code saves the Widget state
     * through the Platform API updateWidgetState(S) method.
     */
    onMapChanged() {
        const center = this.googleMap.getCenter();
        const zoom = this.googleMap.getZoom();
        this.updateWidgetState( { lat: center.lat(), lng: center.lng(), zoom: zoom});
    }

    getDefaultWidgetState(): SampleMapState {
        return {
            zoom: 5,
            lat: NYC_COORDINATES.lat,
            lng: NYC_COORDINATES.lng,
        }
    }