samedi 8 juillet 2017

My function doesnt work as expected the first time it is called but all other calls work as expected

I have a function called _handleSelection which takes a "mode" as a parameter. the mode can be either "SINGLE","RECT" or "LASSO" selection. the the user clicks on the segmented buttons i created the press event of that button calls this function with the parameter.

    this._selectionState = mode; //this variable is declared in the init function

    this._Single = new m.SegmentedButton({
        press: this._handleSelection.bind(this, "SINGLE")
    });

    this._Rectangle = new m.SegmentedButton ({
        press: this._handleSelection.bind(this, "RECT")
    });

    this._Lasso = new m.SegmentedButton({
        press: this._handleSelection.bind(this, "LASSO")
    });


Container.prototype._handleSelection = function(mode) {
    var control = this.getSelected().getContent();
        if (mode === "LASSO") {
            control.setLassoSelection(true);
            this._selectionState = mode;
        } else if (mode === "RECT") {
            control.setRectangularSelection(true);
            this._selectionState = mode;
        } else if (mode === "SINGLE") {
            control.setRectangularSelection(false);
            control.setLassoSelection(false);
            this._selectionState = mode;
        }
};

If the user selects any of the buttons the selection is set in the _handleselection as well as the "this._selectionState" variable.

Now, in my application i have a way for the user to also switch between different views / maps. everytime they switch the view/map the application runs another function. it doesnt call the _handleselection because the user selects a different button to change the view/map. this is why i store the value in the "this._selectionState" variable.

if the user switches the view the following code is called:

        var control = this.getSelectedContent().getContent();
            if (this.getShowSelection()) {
                =Toolbar.addContent(this._selectionMenu);
                if (this._selectionState === "LASSO") {
                    control.setLassoSelection(true);
                }
                if (this._selectionState === "RECT") {
                    control.setRectangularSelection(true);
                }
                if (this._selectionState === "SINGLE") {
                    control.setRectangularSelection(false);
                    control.setLassoSelection(false);
                }

The function is called correctly when i check in the debugger but the problem is on the first time when the user changes the map/view even though the correct selection is turned on (e.g control.getRectangularSelection() = true) the selection is not turned on in the application until the user switches the view to something else and then switches again then everything works as expected from then onwards.

scenario: 1) app loads fine. user in View1. User selects rectangular selection. 2) user uses rectangular selection in view1 fine. 3) user switches to view2. rectangular selection is turned on according to control.getRectangularSelection() but the app is in single selection mode still.

*THE SELECTION SHOULD REMEMBER TO STAY IN RECTANGULAR MODE WHEN YOU SWITCH.

4) user changes back to view1 and rectangularselection is still on as expected. 5) user switches back to view2 and finaly rectangularselection is on.

Aucun commentaire:

Enregistrer un commentaire