reset : Trigger if the collection's all contents have been reset.Passing empty will clear all option or you can specify your options.
Syntax
Syntax
collection.reset([models], [options])
collection.reset()
<!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>
<script
type="text/javascript">
var
obj1 = Backbone.Model.extend({
defaults:
{
name:
"john",
class:
"2"
}
});
var
objCollcetion = Backbone.Collection.extend({model:
obj1});
var
obj2 = new
obj1({ name: "john2",
class: "3"
});
var
obj3 = new
obj1({ name: "john3",
class: "4"
});
var
objCollcetion = new
objCollcetion();
objCollcetion.add([obj2,
obj3]);
document.write('Here
is the result'
+ '');
objCollcetion.each(function
(model, index, list)
{
document.write(model.get('name')
+ '|'
+ model.get('class')
+ '');
});
document.write('---------------------'
+ '');
document.write('Here
is the result after reset'
+ '');
objCollcetion.reset();
objCollcetion.each(function
(model, index, list)
{
document.write(model.get('name')
+ '|'
+ model.get('class')
+ '');
});
</script>
</head>
</html>
Output :
Here is the result
john2|3
john3|4
---------------------
Here is the result after reset
john2|3
john3|4
---------------------
Here is the result after reset
sort : Trigger if collection has been re-sorted.Collection with a comparator
will sort itself whenever a model is added.
Syntax
collection.sort([options])
Example
<!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>
<script
type="text/javascript">
var
student = Backbone.Model.extend({
defaults:
{
name:
"",
class:
""
}
});
var
students =
[
{
name: 'John6',
class: '3'
},
{
name: 'John3',
class: '4'
},
{
name: 'John7',
class: '9'
}
];
var
objCollcetion = new
Backbone.Collection(students,
{
model:
student,
comparator:
'name'
});
document.write('Here
is the sorted result'
+ '');
objCollcetion.each(function
(model, index, list)
{
document.write(model.get('name')
+ '|'
+ model.get('class')
+ '');
});
</script>
</head>
</html>
Output :
Here is the sorted result
John3|4
John6|3
John7|9
change :Trigger if model's attributes have changed.
Example
<!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>
<script
type="text/javascript">
var
student = Backbone.Model.extend({
defaults:
{
name:
"john",
class:
"2"
},
initialize:
function
()
{
this.bind("change:name",
function
(model)
{
document.write('You
have changed your model , chnaged event fired');
});
}
});
var
stuObj = new
student();
document.write('Here
is the result : '
+ stuObj.get("name")+'');
stuObj.set({
name: 'john2'
});
document.write('Here
is the result : '
+ stuObj.get("name")
+ '');
</script>
</head>
</html>
Output :
Here is the result : john
You have changed your model , chnaged event fired
Here is the result : john2
You have changed your model , chnaged event fired
Here is the result : john2
destroy :Trigger if model is destroyed.Destroys the model on the server by delegating an HTTP DELETE
request to Backbone.sync.
Example
<!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>
<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>
</head>
</html>
Output :
create: {"name":"john","class":4}
delete: {"name":"john","class":4,"id":1}
delete: {"name":"john","class":4,"id":1}
No comments:
Post a Comment