Thursday, 14 June 2018

022 BackboneJS Sync


Backbone.Sync


Backbone.js Sync is a function that it is called every time, it attempts to read or save a model to the server.It persists the state of the model to the server.

 

The method signature of Backbone.sync is

    method – The CRUD method ("create", "read", "update", or "delete")


    model – The model to be saved (or collection to be read)


    options – Success and error callbacks.


Example (Create)
<!DOCTYPE html>
<head>
<title>Router Extend Example</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>
<script type="text/javascript">
Backbone.sync = function (method, model)
{
document.write(method + ": " + JSON.stringify(model) + "");
model.set('id', 1);
};

var student = new Backbone.Model
({
name: "john",
class: 4
});

student.save();
student.destroy();
</script>
Output :

create: {"name":"john","class":4}
delete: {"name":"john","class":4,"id":1}


 Example (Fetch)



<!DOCTYPE html>
<head>
<title>Router Extend Example</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>
<script type="text/javascript">
Backbone.sync = function (method, model) {
document.write(method + ": " + JSON.stringify(model) + '');
model.set('id', 1);
};

var student = new Backbone.Model
({
name: "john",
class: 4
});

student.fetch();
</script>
Output :

read: {"name":"john","class":4}  









No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...