Managing Your Backbone Views With the State Pattern
Yesterday I wrote a post to illustrate the concepts behind the State pattern (one of my all time favorite tools). If you’re new to this pattern and haven’t read my previous post I suggest you start there and read this one after you’ve had a chance to play around with the ideas.
I wanted to write about this pattern in the context of a Backbone app because I think there’s a lot of value in exploring different ways to manage our Views and Models. Here’s an example Video Player in which several State objects inherit from a common ancestor, BaseState
. I chose to make BaseState
extend Backbone.Model
because it seemed like the best fit for this kind of thing, although typically when I implement this pattern in other languages I just use generic Objects. I would have done that here but Backbone’s extend
functionality makes the code so much cleaner.
Every state inherits from BaseState
which means they all create a reference to an owner
object in their initialize
methods. The owner
is going to be our actual VideoPlayer
object. Rather than having videoPlayer.play
lead to some big weird conditional:
we’re instead going to delegate the work to our State objects. They’ll handle switching from one State to the next and so long as we provide the exact same public API methods in each, they should be interchangeable. Inheriting from the same BaseState ensures that all of the States have the same public methods and each State can choose how or if it wants to override them. In our example the StoppingState
overrides the enter
method to display some text as we’re transitioning into this state. Again, you can override some or all of the methods, enter
and exit
are great for building up/tearing down anything that our state might need and execute
is where the main work of our state should happen.
Let’s take a look at the VideoPlayer
which is a Backbone.View
that will leverage our State objects:
You’ll notice that VideoPlayer
instantiates its own State objects when it is first created. Then whenever we call one of its public methods the call is delegated to whichever state object is currently residing in this.state
.
Let’s instantiate a new VideoPlayer to see it in action:
Hopefully this gives you some food for thought the next time you’re trying to wrangle some unwieldy component. I cannot count how many times I’ve used this pattern in other languages to tidy up and organize my code. I’m looking forward to working with it again in Backbone and I’d love to hear anyone’s take on how this can be tweaked or improved. Till tomorrow! - Rob
You should follow me on Twitter here.
- Mood: Focused, Hurried
- Sleep: 5
- Hunger: 0
- Coffee: 1