I’m still trying to get my head around Backbone.js
I’ve watched some screencasts and read some tutorials. I also RTFM. But, I’m a little confused on how everyone seems to use parts of the framework to handle different tasks.
I decided to make my first shot at backbone.js in a real app, after playing with the traditional todo list app.
My app will receive a JSON object full of questions. Then, I like to have an ‘ul’ where I can move around those questions (next q, previous q). I already have a view that shows the buttons and notifies whenever a user clicks ‘next’ or ‘prev’. My question is: where should I handle the logic for my ‘currentQuestion’, ‘next’, and ‘previous’ functionality. I’ve seen some folks using a Collection for this, and some other using a Model. It confuses me.
Anyone can point me some meta-code to handle this? Thanks a lot!
There isn’t really a right answer to this question. There is certainly more than one way to make it work and that is one of the nice things about Backbonejs: it is very flexible and it doesn’t impose too many design choices on you.
If I were to start building what you are describing, I would certainly have:
QuestionmodelQuestionscollectionQuestionViewfor rendering out a single questionQuestionsIndexViewfor displaying a list of questionsAfter that is where things get a little fuzzy, and it depends on what your requirements are for the app. If you wanted the state to be stored like a traditional website you might use a router and do something that looks something like this:
This is nice because state is maintained in the URLs so the user can use the browsers fwd and back buttons and things would probably work as they would expect. The problem with this is, how are we supposed to make those
nextQuestionandpreviousQuestionbuttons work?If you make them part of the
QuestionViewa question would have to know what its next and previous questions’ ids were. You could probably come up with a scheme to make this work, but a more elegant and frequently used pattern is to create another model that exists above all of the data models we have mentioned already calledAppand then make theQuestionsCollectionandcurrent_question_idattributes of this model. We would then update thiscurrent_question_idattr in the routers methods.Now we are really cooking, our application state is not only persisted in the URLs of the browser, but it also exists at the application layer as an observable object. We can easily create a
ButtonsPanelViewthat gets passed thisAppmodel and triggers the correct routes when its buttons are clicked. It is also trivial to implementhasNextQuestionandhasPreviousQuestionin theAppmodel and use it totoggleor disable the respective buttons when the user cant go back or forward.EDIT as requested:
To make an
Appmodel that exists above everything else is quite simple. You probably already have code somewhere that looks like this:Just do this instead:
now are
Questionscollection is an attribute of the app model, just as we wanted. So what is the definition forAppgoing to look like? Again, there are many ways to go about this, but I like to use Backbone.Models’s validate to make sure I don’t slip into a bad state. Here’s what I might do: