CodesJava

Easy learning with example program codes

Backbone.JS Sync() collection

The Backbone.JS Sync() collection method is used to specify the state of the model and to display the state of the collection.

Syntax:

Backbone.Collection.Sync ( method, collection, options )   

Parameters:
method: This parameter is used to specify the CRUD operations: create, read, update and delete.
collection: This parameter is used to specify the collection containing a set of models.
options: This parameter is used to specify the success or error messages.

Example:

<!DOCTYPE html>  
<html>
<head>  
<title>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>  
<body>  
<script type="text/javascript">   
Backbone.sync = function(method, model) {  
document.write("Model state:");  
document.write("<br>");  
document.write(method + ": " + JSON.stringify(model));  
};  
var X = new Backbone.Collection({  
name: "Tom",  
age: 20
});   
X.fetch();  
</script>  
</body>  
</html>

Output:

Model state:
read: [{"name":"Tom","age":20}]

Explanation:
In the above example, sync() method reads and fetches the model data while the fetch() method display the model’s state by delegating the sync() method.

Please follow and like us:
error









Copyright © 2019 CodesJava DMCA.com Protection Status