2

Backbone tutorials I have read implement some type of a mini-framework (i.e. Slim) with a RESTful architecture performing CRUD on a server db, like this. The Backbone docs state you need a RESTful api, which I believe is due to the Backbone Route and Sync functionality that keeps models up to date, which is a huge aspect of my choosing to use Backbone.

For example, the line below maps a faux url (route) to the 'addWine' function (within a Slim api):

$app->post('/wines', 'addWine');

Assumption 1: If I have a (PHP) CMS backend (and not a mini-framework) I assume I could simply replace the 2nd parameter (addWine) with my own CMS class method call and return a json object.

Assumption 2 But I would not be able to directly call that same class method from a link in the html without causing backbone to lose state and thus it's ability to sync the model data (and remember the browsers history).

Assumption 3 In that case, I will need to use the Slim api and route backbone urls through (Slim) RESTful CRUD calls in order to access my CMS database to keep backbone happy.

If those assumptions are correct, then it would seem backbone is intercepting those HTTP calls - which leaves me wondering how the whole RESTful + Backbone relationship works. Can you explain some of it?

If my assumptions are incorrect, then I need more help than I thought. Can you help with that?

Thanks

Ricalsin
  • 950
  • 9
  • 28

2 Answers2

1

I can't speak intimately to your three assumptions, but as for your final question -- Backbone does not "intercept" HTTP calls -- it constructs them, just as any other javascript library would to create an AJAX request.

Backbone is relatively agnostic to your server side language/framework. Here is what Backbone expects any time "sync" is called:

Backbone's sync function uses different HTTP request types based on which method was called. These different HTTP request types are:

  • POST
  • GET
  • PUT
  • DELETE

Your framework needs to support all of the above to support the "out of the box" functionality of Backbone. This means that you must specify all of the above routes within your application in order to work with Backbone.

One other thing to note is the "create" and "update" method does not carry post data with the request specifically -- instead it sends a content body with a json digest of the data and expects the server side to properly parse a JSON object and deal with it appropriately.

Andy Baird
  • 6,088
  • 4
  • 43
  • 63
  • For those struggling with a RESTful (Representational State Transfer) architecture, [read this](http://www.infoq.com/articles/webber-rest-workflow). Backbone requires a REST implementation in order to work. Andy's note about POST and JSON along with the link should help to build the picture of what needs to happen. As @Claudio-Hojda confirms, all three of my assumptions were correct; but websockets and polling the server are only needed if you wish to keep other people up to date on the server's status - which was not my goal here. – Ricalsin Jul 30 '12 at 14:53
1

I say yes to all three assumptions and also agree with @Andy Baird.

Also, the only problem to your project is how to notify Backbone that you have updated the database and you would like it to update itself in the front-end. I can only see two solutions:

1) using Javascript's setInterval() - if you do not need the front end to be updated immediately on DB update, you can check for changes every 1 minute, Backbone knows to only update what has changed and add new stuff but of course this is not healthy to the server if you have 1k active people making repeated request every minute

2) using SocketIO or similar service - this way you can send from the server to Backbone either the entire list of modifications to your DB or a simple 'Please refresh, new stuff waiting'. Check this discussion.

Community
  • 1
  • 1
Claudiu Hojda
  • 1,021
  • 9
  • 14