sebpiq / backbone.statemachine

Simple finite-state machine for Backbone. View states made easy. Synchronizing your application's parts with events made easy.
MIT License
177 stars 16 forks source link

Error on initial toState with StatefulView #13

Closed dukejones closed 12 years ago

dukejones commented 12 years ago

When I try to enter the initial state this.toState('placing') This line: this.stateClassName = (this._states[name].className || name); throws the error Uncaught TypeError: Cannot read property 'placing' of undefined

What gives? Looks like _states is not being initialized. I'm trying to define the state callbacks by setting the "states" hash on the object, but that doesn't seem to make a difference in terms of the error being thrown.

sebpiq commented 12 years ago

Hi Duke ! Could you post the code you're executing ? It is a bit hard to guess what's wrong just like that.

sebpiq commented 12 years ago

No news, good news :)

dukejones commented 12 years ago

FYI: ended up using the state machine as a mixin.

    _.extend(@, Backbone.StateMachine)
    @startStateMachine()
    @toState('rendering')
sebpiq commented 12 years ago

Ok ... so you didn't manage to make StatefulView work ? If so there might be a bug and it would be really nice if you could post the code you were using. Otherwise I can't fix it, nor help you get it right !!!

dukejones commented 12 years ago

I'm mainly a Ruby programmer, and there are times when inheritance feels right, and there are times when simply mixing in the functionality feels right. To me, this seems like the latter. I don't want to say, "this view is a _____"; I want to say, "give this view state machine functionality".

I'm thinking it would be nice to have a convenience function for mixing in state machine functionality. I may try my hand at that in the next couple of weeks.

But meanwhile I'll go ahead and update to the latest backbone.statemachine, and swap out the mixin code for the inheritance object, and post what errors pop up. (May take me some days to get to it.)

sebpiq commented 12 years ago

swap out the mixin code for the inheritance object, and post what errors pop up.

Ok great !

For me the 2 paradigms are good for different things. I like mixins, except that mixins are just "incomplete" objects (which implement some functionality, they are highly reusable, but are not standalone). Then, you mix them in you make a "complete" object (more specialized, which you cannot use as a mixin anymore). I guess that's the model that has been used in Backbone ... so I'll stick to it.

StateMachine is a mixin like Events is, StatefulView is a class like View is.

dukejones commented 12 years ago

Ahh, thanks for the clarification.

dukejones commented 11 years ago

Hey, got the Backbone.StatefulView working. One thing though: in the docs, it still says you need "toState" and doesn't mention the init state. I kind of prefer toState over requiring init... since you have to trigger('initialized') anyway, and then you have to set up init transitions. How about if it doesn't require init, but uses it if it's present?

sebpiq commented 11 years ago

Hey, got the Backbone.StatefulView working.

Cool

How about if it doesn't require init, but uses it if it's present

Doc says "The state machines are always created in init state, even if it isn't declared". The machine is initialized in state init, but if you want you can just do this.toState('yourFavoriteState') on start-up.

dukejones commented 11 years ago

Yeah, I tried that, but it throws an error if I don't have any state transitions defined for "init".

sebpiq commented 11 years ago

Ok ... this will be fixed (see #20)

dukejones commented 11 years ago

thanks!