Insert Backbone.Js Model into MySQL Database

Insert Backbone.js model into MySQL database

Backbone is based on a REST API: when saving/updating a model to the server, Backbone will send it serialized as JSON in the request body with a POST our PUT request. From Backbone.sync documentation

With the default implementation, when Backbone.sync sends up a request
to save a model, its attributes will be passed, serialized as JSON,
and sent in the HTTP body with content-type application/json.

This means that server-side you have to

  • determine the type of request
  • decode the serialized JSON

Something like this should get you started

$request_method = strtolower($_SERVER['REQUEST_METHOD']);
$data = null;

switch ($request_method) {
case 'post':
case 'put':
$data = json_decode(file_get_contents('php://input'));
break;
}

// print_r($data);

// note that mysql_* functions are deprecated
// http://php.net/manual/en/function.mysql-query.php
// inserting with a PDO object, assuming an auto incremented id
$sql = "INSERT INTO boxes (x, y, w, h) VALUES(?, ?, ?, ?)";
$sth = $dbh->prepare($sql);
$sth->execute(array(
$data->x,
$data->y,
$data->w,
$data->h
));
$id = $dbh->lastInsertId();

Check this page for a more thorough implementation of a REST API in PHP http://www.gen-x-design.com/archives/create-a-rest-api-with-php/

How to use backbone.js with php to save data into mysql database

When you call the .save() method a JSON object is sent to the url you have specified; if the model is new, the POST method will be used. When the JSON is received in the PHP script you have to decode it and save the model attributes, let us say one field per attribute. Change this in the 'inserts.php':

<?php 
include('conn.php');

$data = json_decode(file_get_contents('php://input'));
$name = $data->{'name'};
$password = md5($data->{'password'});
$email = $data->{'email'};

//proceed to add every variable to a field in the database
//...

Store data in mysql using backbone.js and with other server scripting lanuage

assuming the data is saved in model using

model.save([attributes], [options]) 

will Save a model to your database

refer this : http://backbonejs.org/#Model-save

How to save Backbone.js model to database

Unless you are using HTML5 local storage in the client the responsibility for saving to the database is not with backbone.js. Backbone will talk to the server via Backbone.sync using a REST type request. Effectively it will make an http POST request to save a new record or an http PUT request to update a current record.

The difference between a new record and a current record is that the :id field of the record is not set for a new record and is set for an old record.

If you want a tutorial using Ruby Rails as your backend solution then you can look at this tutorial.

http://www.jamesyu.org/2011/01/27/cloudedit-a-backbone-js-tutorial-by-example/

However you can use any backend server such as PHP, Java, Django etc as long as they meet the requirements of the REST interface that backbone.js uses.

If you overide Backbone.sync you can also get backbone.js to interface to pretty much any legacy http protocol as well.

Sync backbone.js to php/MySQL

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.



Related Topics



Leave a reply



Submit