Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing the currentActionArgs variable #112

Closed
george-lin opened this issue Oct 19, 2015 · 1 comment
Closed

Accessing the currentActionArgs variable #112

george-lin opened this issue Oct 19, 2015 · 1 comment

Comments

@george-lin
Copy link

Hello,

First of all, thank you for the library.

I am trying to pass parameters from one state to another state.

Below is a very simple example to demonstrate passing parameter into the toggle function, and getting used (console.log) in the _onEnter of the next state.

As you can see, I am digging into the internals of the __machina__ property to gain access of the currentActionArgs variable. Accessing stuff in a property with double underscore before and after just feels wrong.

What is the best way to retrieve the arguments passed in?

Thanks.

var behavioralFsm = new machina.BehavioralFsm({
    initialState: "start",
    states: {
        "start": {
            _next: "on"
        },
        "on": {
            _onEnter: function(client, args) {
                client.history.push("turning on " + args);
                console.log('on.enter');
                console.log(getParameter(client));
            },
            _next: "off"
        },
        "off": {
            _onEnter: function(client, args) {
                client.history.push("turning off " + args);
                console.log('off.enter');
                console.log(getParameter(client));
            },
            _next: "on"
        },

    },
    toggle: function(client, args) {
        this.handle( client, '_next', args );
    }
});

var client = {
    history: []
};

function getParameter (fsm) {
    for (var item in fsm.__machina__) {
        return fsm.__machina__[item].currentActionArgs;
    }
}

behavioralFsm.toggle(client, 1);
behavioralFsm.toggle(client, 2);
behavioralFsm.toggle(client, 3);
behavioralFsm.toggle(client, 4);

console.log(client.history);
@ifandelse
Copy link
Owner

@george-lin Sincere apologies my long absence here (see #146). The _onEnter handlers do not accept additional arguments currently. It's something I've been asked about before, and will consider looking into for the next major version bump. The toggle function is actually telling the FSM to handle the _next input, so the argument you're passing is going to the _next handler. If you were using the function syntax as opposed to the string state name shortcut, you could console log args and see the integer being passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants