Backbone communicates with backend web services using the Backbone.sync function; this function examine the model object being synchronised to determine whether the operation is a “create”, “read”, “update” or “delete” and then uses jQuery.ajax to perform a HTTP POST, GET, PUT or DELETE on the backend web service. The change I want to make is to use PROPFIND and PROPPATCH instead of GET and PUT. I may go into the reasons for this change more in subsequent posts, but for now just trust me (please) that there is a reason why I want to do this.
One of the reasons that I chose Backbone was this section from the “Extending Backbone” section in the documentation:
Many JavaScript libraries are meant to be insular and self-enclosed, where you interact with them by calling their public API, but never peek inside at the guts. Backbone.js is not that kind of library.So here goes! The Backbone.sync function is function (method, model, options) where method = “create” | “read” | “update” | “delete”. Looking at the source of the function, the first line of the function does this:
Because it serves as a foundation for your application, you're meant to extend and enhance it in the ways you see fit
var type = methodMap[method];
and the definition of methodMap is:
var methodMap = {
'create': 'POST',
'update': 'PUT',
'patch': 'PATCH',
'delete': 'DELETE',
'read': 'GET' };
So to use PROPFIND and PROPPATCH instead of GET and PUT I should be able to simply create a different MyApp.methodMap hash (the original variable is private to the anonymous function that defines the Backbone namespace and functions), create a new MyApp.sync function which is an exact copy of Backbone.sync but referencing my new MyApp.methodMap hash and replace Backbone.sync with MyApp.sync. Something like this:
MyApp.methodMap = {'create': 'POST', 'update': 'PROPPATCH', 'patch':'PATCH', 'delete': 'DELETE', 'read':'PROPFIND'};
MyApp.sync = function (method, model, options) {
var type = MyApp.methodMap[method];
// all the rest of Backbone.sync
};
Backbone.sync = MyApp.sync;
But it doesn’t work. Monitoring the HTTP traffic using Fiddler confirms that the PROPFIND verb is being sent correctly when I call Model.fetch() but my model isn’t actually being populated with the data returned. Looking further into the original Backbone.sync function, on about the 42nd line it decides whether to process the returned data based upon the HTTP verb that it’s sending:
// Don't process data on a non-GET request.
if (params.type !== 'GET' && !options.emulateJSON) {
params.processData = false;
}
I just have to replace the ‘GET’ with a ‘PROPFIND’ and my model objects are populated correctly.
So to recap, all I had to do to make Backbone use PROPFIND and PROPPATCH instead of GET and PUT was to:
- Create a new MyApp.sync function and MyApp.methodMap hash that were exact copies of the originals.
- Modify the new MyApp.sync function to reference MyApp.methodMap.
- Modify MyApp.methodMap to return PROPFIND for read and PROPPATCH for update.
- Modify MyApp.sync to process returned data for PROPFIND instead of GET.
Awesome article. It is so detailed and well formatted that i enjoyed reading it as well as get some new information too.
ReplyDeleteSelenium training in Chennai
Selenium training in Bangalore
Hello, simply a second returning I was searching for the critical focuses on the same subject and now I am here. So much data, truly decently utilized weblog website page.keep it up!!
ReplyDeleteandroid training in chennai
android online training in chennai
android training in bangalore
android training in hyderabad
android Training in coimbatore
android training
android online training
mmorpg oyunlar
ReplyDeleteinstagram takipçi satın al
tiktok jeton hilesi
Tiktok Jeton Hilesi
antalya saç ekimi
referans kimliği nedir
instagram takipçi satın al
instagram takipçi satin al
metin2 pvp serverlar
Tül Perde Modelleri
ReplyDeleteMobil Onay
mobil ödeme bozdurma
nft nasıl alınır
ankara evden eve nakliyat
Dedektör
WEBSİTESİ KURMAK