FrozenCanuck / Ki

A Statechart Framework for SproutCore
http://frozencanuck.wordpress.com
Other
105 stars 7 forks source link

Send an optional context object along with gotoState #4

Closed devinus closed 13 years ago

devinus commented 13 years ago

Are there philosophical objections to sending along a context with gotoState that enterState could use? E.g.:

gotoState: function(state, fromCurrentState, useHistory) {

Becomes:

gotoState: function(state, fromCurrentState, context, useHistory) {

Then:

enterState: function (context) {

This would help us simplify a lot of things.

FrozenCanuck commented 13 years ago

Hmm, ya. From a purity perspective there's nothing mentioned about passing args; however, passing arguments when going through a state transition process needs to be looked at carefully.

When you go to a state and pass args what does that mean? How does that affect all states that are being entered during the state transition process? What about parallel states?

Could you provide some more detail about what you are trying to do?

devinus commented 13 years ago

Yes--and just as an example--you wanted to pass along parameters from SC.Routes into your state transition, e.g.:

_detailRoute: function (params) {
    this.gotoState('READY.DETAIL', this, { id: params.id });
}

We've run into a few frustrating scenarios where being able to pass along a context would greatly simplify things.

devinus commented 13 years ago

gotoState(state, fromCurrentState, context, useHistory) as a bonus would resemble the function signature for sendEvent. :)

FrozenCanuck commented 13 years ago

Okay. Let me put together an experimental branch that will allow you pass args to the enterState function.

devinus commented 13 years ago

Great, I'll begin testing it as soon as it's available!

FrozenCanuck commented 13 years ago

The changes are up on the experimental enter-state-args branch. I'm still debating the implementation, but let me know how this is working out for you in the mean time.

devinus commented 13 years ago

We've started using the branch, and it really is working great. Now instead of using events to pass a context after we enter states, which is essentially what we wanted to do when we entered the state, we can simply pass it on enter. Really awesome.

FrozenCanuck commented 13 years ago

Glad to hear it's working out for you. I'm thinking about refactoring the implementation. The reason is because the context argument must be supplied when calling the statechart's gotoState whenever you supply the useHistory argument. In such cases the value someone would likely set on the context arg would be null, but null is a legitimate value one might expect when entering a state. Rather, if the context argument is not intended to be used then the context value passed to the enterState should be the undefined value.

devinus commented 13 years ago

Right now our gotoStates look like this:

this.gotoState('READY.DETAILS', null, { id: params.id })

Are you proposing:

this.gotoState('READY.DETAILS', null, NO, { id: params.id });
devinus commented 13 years ago

Have you thought any more about your proposal?

FrozenCanuck commented 13 years ago

Sorry. I've been so side tracked with other stuff that I have neglected to give my full attention to the matter. Regarding my proposal, ya, I was thinking of:

this.gotoState('READY.DETAILS', null, NO, { id: params.id });

Therefore if the user does not want to supply a context value then undefined will be supplied to the enterState function. The value null can be considered a legit value, so I rather avoid passing null as a default value.

devinus commented 13 years ago

That makes sense.

jeremi commented 13 years ago

Is there any chance to see this in sproutcore now that KI is the official statechart ? I'm having a similar issue. My state need to have a context to pass an id.

FrozenCanuck commented 13 years ago

Yep. Updates are coming soon for both Ki frameworks (on my github account and on the SproutCore account).

FrozenCanuck commented 13 years ago

implemented