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 =
</ script >
< script src =
type = "text/javascript" >
</ script >
< script src =
type = "text/javascript" >
</ script >
</ head >
< body >
< h1 style = "color: green;" >
GeeksforGeeks
</ h1 >
< h3 >Backbone.js Sync emulateJSON</ h3 >
< script type = "text/javascript" >
Backbone.emulateJSON = true;
var newCol = new Backbone.Collection({
title: "GeeksforGeeks",
about: "A computer science portal for Geeks!!"
});
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 =
type = "text/javascript" >
</ script >
< script src =
type = "text/javascript" >
</ script >
< script src =
type = "text/javascript" >
</ script >
</ head >
< body >
< h1 style = "color: green;" >
GeeksforGeeks
</ 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
Last Updated :
10 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...