-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollection.reset.html
More file actions
49 lines (49 loc) · 1.85 KB
/
Copy pathcollection.reset.html
File metadata and controls
49 lines (49 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script src="jquery-1.11.2.js"></script>
<script src="underscore.js"></script>
<script src="backbone.js"></script>
<script> // Line 42 isn't true, but should be
var count = 0;
var mytodo = new Backbone.Model();
var TodosCollection = new Backbone.Collection([]);
// we can listen for reset events
//TodosCollection.on("reset", function() {
// console.log("Collection reset.");
//});
TodosCollection.add([
{ title: 'go to Jamaica.', completed: false },
{ title: 'go to China.', completed: false },
{ title: 'go to Disneyland.', completed: true }
]);
TodosCollection.on('reset', function(TodosCollection, options) {
console.log("Reset Event Listener Fired");
_.each(options.previousModels, function(model){ // Foreach through options.previousModels
console.log("Foreach Loop");
console.log(model.toJSON());
//model.trigger('remove');
console.log('Model: ' + model.get('title'));
count++;
console.log(count);
});
console.log('options.previousModels: ' + options.previousModels);
console.log('options.previousModels[0]: ' + options.previousModels[0]);
console.log('JSON stringified options.previousModels: ' + JSON.stringify(options.previousModels));
console.log(options.previousModels === TodosCollection[0]); // should be true, but isn't
//console.log('JSON stringified: ' + TodosCollection.toSource()); // Prints out this function, for some reason
});
console.log('Collection size: ' + TodosCollection.length); // Collection size: 3
//TodosCollection.reset([ // Reset can also be used to add/remove/change collections
// { title: 'go to Cuba.', completed: false }
//]);
TodosCollection.reset([]);
console.log('Collection size: ' + TodosCollection.length); // Collection size: 0
console.log('Collection: ' + TodosCollection);
</script>
</body>
</html>