In which I explore creating a RESTful API using Node.js, Express, MongoDB with Mongoose, and then I use Backbone.js to interact with the web service.
-
Read more about schema types
-
Fiddle with the web service in the console by creating a new product:
jQuery.post("/api/products", { "title": "My Awesome T-shirt", "description": "All about the details. Of course it's black.", "style": "12345" }, function (data, textStatus, jqXHR) { console.log("Post resposne:"); console.dir(data); console.log(textStatus); console.dir(jqXHR); });
-
or by reading the product data previously created (this request reads all products)
jQuery.get("/api/products/", function (data, textStatus, jqXHR) { console.log("Get resposne:"); console.dir(data); console.log(textStatus); console.dir(jqXHR); });
-
or by reading a specific product (modify the uniquely generated ID)
jQuery.get("/api/products/50339f8223370e810a000001", function(data, textStatus, jqXHR) { console.log("Get resposne:"); console.dir(data); console.log(textStatus); console.dir(jqXHR); });
-
or by updating using PUT
jQuery.ajax({ url: "/api/products/50339f8223370e810a000001", type: "PUT", data: { "title": "My Awesome T-shirt in Black", "description": "All about the details. Of course it's black, and long sleeve", "style": "12345" }, success: function (data, textStatus, jqXHR) { console.log("Post resposne:"); console.dir(data); console.log(textStatus); console.dir(jqXHR); } });
-
or finally by deleting the product
jQuery.ajax({ url: "/api/products/50339f8223370e810a000001", type: "DELETE", success: function (data, textStatus, jqXHR) { console.log("Post resposne:"); console.dir(data); console.log(textStatus); console.dir(jqXHR); } });
-
Backbone uses a sync object to interact with the API
-
Each product in the #list represents data found in the model
-
The product #list represents a collection of (product) models
-
The HTML is generated using a view which renders data - blending the model's JSON data with a template
-
A route triggers the asynchronous process of fetching data and rendering the product #list by listening for a hashchange or using pushState
-
Look into Backbone's Routers, Models, Views, Collections, Backbone.sync and Backbone.history
-
Also, jQuery deferred object, Mustache