I am providing a second answer to simplify the code that you need to learn to get the main points that you are thinking about - the actual round from model to server and how identifiers play their part.
Suppose you have defined a model - release Jurassic Park.
// Define your model var Dinosaur = Backbone.Model.extend({ defaults: { cavemanEater: undefined // Has one property, nom nom or not. }, urlRoot: 'dino' // This urlRoot is where model can be saved or retrieved }); var tRex = new Dinosaur({'cavemanEater':true});
Now you have created a copy of the dinosaur, which is food. The roar.
console.log(tRex);
What you should notice is that in the tRex properties your model does not have an identifier. Instead, you will see the cID, which can be considered a temporary identifier that Backbone automatically assigns to your models. When a model does not have an identifier, it is considered new. The concept of saving the model (either in the database or in local storage) allows you to return to this resource after you created it and do something like save (PUT) or destroy (DELETE). It would be difficult to find this resource if you had no way to point directly to it! To find this resource, your model needs an identifier, something is currently missing.
Since the above answers explained that this is the work of your database (or localstorage or some other solution), to provide a Backbone resource identifier. In most cases, this comes from the resource identifier itself, aka the identifier of the primary key of your model in some table.
With my installation, I use PHP and mySQL. I will have a table called "Dinosaur", and each row will be a constant representation of my dinosaur model. This way I will have an id column (unique auto-incrementing int) and cavemanEater (bool).
The flow of data is as follows.
- You are creating a model.
- The model is new, so it only has the identifier cID - there is no proper identifier.
- You save the model.
- json representation of your model sent to your server (POST)
- Your server stores it in a table and assigns it a resource identifier.
- SENDS BACK server presents a json representation of data {id: uniqueID}
- The backbone will GET this json view with id
- Backbone automatically updates your model with an identifier.
Here's what the annotated code looks like.
CLIENT:
tRex.save(); // {'cavemanEater':true} is sent to my server // It uses the urlRoot 'dino' as the URL to send. eg http://www.example.com/dino
SERVER:
// Is setup to accept POST requests on this specific ROUTE '/dino' // Server parses the json into something it can work with, eg an associative array // Server saves the data to the database. Our data has a new primary id of 1. // Data is now persisted, and we use this state to get the new id of this dino. $dinoArray = array('id'=>1, 'cavemanEater'=>true); $dinoJSON = json_encode($dinoArray); // Server does something to send $dinoJSON back.
CLIENT:
// If successful, receives this json with id and updates your model.
Now your tRex has id = 1. Or should I say ...
tRex.toJSON(); // RETURNS {'id':'1', 'cavemanEater':'true'}
Congratulations. If you do this tRex.isNew() , it will return false.
The highway is smart. He knows POST new models and PUT models that already have a resource identifier.
Next time you will do the following:
tRex.save();
The trunk will make a PUT request to the following URL.
http://www.example.com/dino/1
This is the default behavior. But you will notice that the URL is different than saving. On the server, you need a route that accepts / dino /: id as opposed to / dino
It will use the route pattern / urlRoot /: id for your default models, unless you change it otherwise.
Unfortunately, the dinosaurs became extinct.
tRex.destroy();
It will cause ... Can you guess? Yeah. DELETE query / dino / 1.
For Backbone to work, your server must distinguish between different requests for different routes. There are several server-side technologies that can do this.
Someone mentioned Sinatra if you are using Ruby. As I said, I use PHP and I use SLIM PHP Framework. He is inspired by Sinatra, so he is like, and I like it. The author writes clean code. How these RESTful server implementations work is beyond the scope of the discussion.
I think this is the basic complete transfer of new Backbone data without id, via the Internet to your server, where it generates, and sends back the resource identifier so that your model lives happily. (Or destroy() not ...)
I donβt know if this is too new for you, but hopefully this will help someone else who is facing this problem. The highway is very interesting for programming.
Other Similar Answers: Ways to Save JB Base Model Data