Backbone.js Sync emulateJSON
Backbone.js is a compact library used to organize JavaScript code. An MVC/MV* framework is another term for it. If MVC is unfamiliar to you, it is just a technique for designing user interfaces. The creation of a program’s user interface is made considerably easier by JavaScript functions. BackboneJS provides a variety of building elements to aid developers in creating client-side web applications, including models, views, events, routers, and collections.
Sync’s emulateJSON is mainly used in the cases where the web server cannot handle encoded application/JSON requests. This method is mainly useful in the case of using a legacy web server. The activity of this method is to serialize the JSON under a model and process it as if it is an HTML form.
Syntax:
Backbone.emulateJSON=true
Example 1: The code below demonstrates syncing of a collection and how we can use the emulateJSON() method, so we can easily serialize the JSON under the model.
HTML
<!DOCTYPE html> < html > < head > < title >Backbone.js Sync emulateJSON</ title > < script src = "https://code.jquery.com/jquery-2.1.3.min.js" type = "text/javascript" > </ script > < script src = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" type = "text/javascript" > </ script > < script src = "https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" type = "text/javascript" > </ script > </ head > < body > < h1 style = "color: green;" > w3wiki </ h1 > < h3 >Backbone.js Sync emulateJSON</ h3 > < script type = "text/javascript" > Backbone.emulateJSON = true; var newCol = new Backbone.Collection({ title: "w3wiki", about: "A computer science portal for Beginner!!" }); Backbone.sync = function (method, model) { document.write("The state of the Collection:"); document.write("< br >"); document.write(method + ": " + JSON.stringify(model)); }; newCol.fetch(); </ script > </ body > </ html > |
Output:
Example 2: The code below demonstrates syncing of a model and how we can use the emulateJSON() method so we can easily serialize the JSON under the model.
HTML
<!DOCTYPE html> < html > < head > < script src = "https://code.jquery.com/jquery-2.1.3.min.js" type = "text/javascript" > </ script > < script src = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" type = "text/javascript" > </ script > < script src = "https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" type = "text/javascript" > </ script > </ head > < body > < h1 style = "color: green;" > w3wiki </ h1 > < h3 >Backbone.js Sync emulateJSON</ h3 > < script type = "text/javascript" > Backbone.emulateJSON = true; Backbone.sync = function(method, model) { document.write(method + ": " + JSON.stringify(model)); model.set('New', "Kotlin"); }; var progLang = new Backbone.Model({ Old:"Basic", }); progLang.save(); progLang.save({Popular: "JavaScript"}); </ script > </ body > </ html > |
Output:
Reference: https://backbonejs.org/#Sync-emulateJSON
Contact Us