Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sync backbone.js to php/MySQL

In designing my app, I have organized my UI with backbone.js, with each model representing an entry of data in MySQL DB. I understand how to receive user inputs and modify model accordingly. However, how do you SYNC this back to the DB achieving CRUD (Create, Read, Update Delete) with REST?

I've seen implementations done with Rails 3. However, I only know javascript, jQuery, php, backbone.js. I would like to know the fastest way to do this without learning a whole know language Ruby for this one task.

like image 976
William Sham Avatar asked Dec 17 '22 11:12

William Sham


1 Answers

The first thing that I would like to seriously emphasize that Backbone.js is definitely not hardwired to any specific server side architecture viz rails. Many (most?) of the Backbone.js enthusiasts are also rails enthusiasts as Backbone.js is designed in harmony with restful patterns which rails community is very passionate about.

But if you dont want to use Rails, you can still take absolutely the FULL advantage of Backbone.js

First and foremost, decide if you want to implement a restful interface. Doing so is very easy in php. If you are using zend framework then you have the excellent Zend_Rest component to assist you in creating a powerful restful api. Alternatively you might want to look into comparitively new frameworks like Recess and Fuel that have built in support for creating restful apis conveniently with ease.

You can ofcourse code your own restful api through vanilla php.

Please note that following a few conventions will make Backbone integration very convenient. Eg. backbone expects records to have an id field, also backbone expects that upon submission of a record server will return a json serialized updated record with updated fields, which will be used to update the client side model. Taking care of some such small things will enable you to create your application without overriding much of backbone's classes.

Now, if your rest api interface differs radically from the rails implementation or you are not implementing a rest api at all you will have to reprogram the Backbone.Model.sync function to suit your requirements.

like image 122
lorefnon Avatar answered Dec 19 '22 08:12

lorefnon