William Sham
William Sham

Reputation: 13239

Apply Backbone.js for multi-module Javascript Website

I am writing a module based javascript website, backed by a Mysql DB.

Each module talks with the DB through PHP. On the UI, for simplicity, the module on the left will display all relevant rows, with edit(write) functionality. The module on the right display the data from the same DB, with write access too. So each would affect the other in each update.

I'm told backbone would be a good framework. Then, I have read the todos example, and understand how there are item->itemList, view->viewList, etc...

But now, the question is, how do I apply it to this situation?

Which is an item, a view in this situation?

Upvotes: 0

Views: 292

Answers (1)

M. Lanza
M. Lanza

Reputation: 6790

The module on the left and the module on the right are Views, each of which can be made up of other views.

Within the model don't store a reference to the view (as I've seen done in some of the examples):

this.view = view; //view being passed in as an arg

The reverse (a view storing a reference to a model) is okay. Your views should be doing most of the work, listening to and responding to model events. Thus, in the view initialize method you might:

model.bind("interesting-event", function(){
  //the view updates/reacts to the model.
});

Also, never add a model to two collections (just one ever). When a model is assigned to a collection, Backbone sets a reference on the model that points to the owning collection.

Incidentally, the a-model-cannot-belong-to-two-collections issue is the reason why you don't want a model referencing its view. A model can have many views on one screen.

Backbone is perfect for your needs. Start with a very basic version of your app and keep fleshing it out. All the while keep reading about Backbone online. I read everything I could find (there's not a whole lot, not really). The key concept is simply event based programming (much like you'd use in VB or lots of other platforms). It's a lot of trial and error, but you'll make sense of it with practice.

Upvotes: 1

Related Questions