HTML and CSS Reference
In-Depth Information
In the above code, the urlRoot for window.Book is the RESTful service endpoint to
retrieve or persist model data. Note that this attribute is needed only when retrieving
or persisting models that are not part of a collection. If the Model is part of a collec‐
tion , the url attribute defined in the collection is enough for Backbone to know how to
retrieve, update, or delete data using your RESTful API.
In window.BookCollection , url provides the endpoint for the RESTFul API. This is all
that's needed to retrieve, create, update, and delete with Backbone's simple Model API.
If your persistence layer is not available through RESTful services, or if you would like
to use a different transport mechanism such as WebSockets, you can override Back
bone.sync .
Backbone.sync is the function that Backbone calls every time it attempts to read or save
a model to the server. By default, it uses jQuery or Zepto to make a RESTful JSON request.
You can override it to use a different persistence strategy, such as WebSockets, XML
transport, or localStorage . 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 . The default sync han‐
dler maps CRUD to REST like so:
create -> POST /collection
read -> GET /collection[/id]
update -> PUT /collection/id
delete -> DELETE /collection/id
Backbone and legacy servers
If you must work with a legacy web server that doesn't support Backbones's default
REST/HTTP approach, you may choose to turn on Backbone.emulateHTTP . Setting this
option will fake PUT and DELETE requests with a HTTP POST , setting the X-HTTP-Method-
Override header with the true method. If emulateJSON is also on, the true method will
be passed as an additional _method parameter. For example:
Backbone . emulateHTTP = true ;
model . save (); // POST to "/collection/id", with "_method=PUT" + header.
If you're working with a web server that can't handle requests encoded as application/
JSON, setting Backbone.emulateJSON = true; will cause the JSON to be serialized
under a model parameter, and the request to be made with a application/x-www-form-
urlencoded mime type , as if from an HTML form.
Ember.js (formerly Amber.js and SproutCore 2.0) is one of the newest contenders. It is
an attempt to extricate the core features from SproutCore 2.0 into a more compact
modular framework suited for the Web. It's also well known for gracefully handling
DOM updates and has a respectable following on github ( Figure 4-12 ).
Search WWH ::

Custom Search