The Backbone.JS Save() model is used to read and save the data of the model every time when it is called.
Syntax:
Model.Save (attributes, options)
Model.Save (attributes, options)
Model.Save (attributes, options)
Parameters:
attributes: This parameter is used to specify the properties of a model.
options: This parameter is used to specify the options like id, name etc. for a model.
Example:
<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>
<script type="text/javascript">
var details = new Backbone.Model({
msg1: "HELLO WORLD.",
msg2: "Welcome."
});
Backbone.sync = function(method, model) {
document.write(method + ": " + model.get('msg1')+ " " +model.get('msg2'));
model.set('id', 100);
};
details.save();
document.write("<br>");
details.save({msg1 : "Hello World."});
</script>create: HELLO WORLD. Welcome.<br>update: Hello World. Welcome.
<!-- Mirrored from www.w3schools.blog/backbonejs-save by HTTrack Website Copier/3.x [XR/YP'2000] -->
<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>
<script type="text/javascript">
var details = new Backbone.Model({
msg1: "HELLO WORLD.",
msg2: "Welcome."
});
Backbone.sync = function(method, model) {
document.write(method + ": " + model.get('msg1')+ " " +model.get('msg2'));
model.set('id', 100);
};
details.save();
document.write("<br>");
details.save({msg1 : "Hello World."});
</script>create: HELLO WORLD. Welcome.<br>update: Hello World. Welcome.
<!-- Mirrored from www.w3schools.blog/backbonejs-save by HTTrack Website Copier/3.x [XR/YP'2000] -->
Example create: HELLO WORLD. Welcome.
update: Hello World. Welcome.
Output:
create: HELLO WORLD. Welcome.
update: Hello World. Welcome.
create: HELLO WORLD. Welcome.
update: Hello World. Welcome.
create: HELLO WORLD. Welcome. update: Hello World. Welcome.
Explanation:
In the above example, the properties of a model are created, updated and saved.