forked from joaool/MotherGithub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone4.html
59 lines (55 loc) · 2.34 KB
/
backbone4.html
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
50
51
52
53
54
55
56
57
58
59
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Backbone v4.0</title>
</head>
<body>
<!--<script type="text/javascript" charset="utf-8" src="../rivets/dist/rivets.js" ></script>-->
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<!--<script type="text/javascript" charset="utf-8" src="../q-1/q.js" ></script>
<script type="text/javascript" src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
<script type="text/javascript" src="http://documentcloud.github.com/backbone/backbone-min.js"></script>-->
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js" type="text/javascript"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js" type="text/javascript"></script>
<script type="text/javascript">
var Person = Backbone.Model.extend({
initialize: function(){
console.log("Person is initialized.");
this.bind("change:name", function() { //any time a change happens to the model, we can automatically update the view
console.log(this.get("name") + " is now the value for name");
});
},
defaults:{
name:"undefined",
age:"undefined"
// id:0 //id is a special attribute. no 2 models can have the same id
}
});
var People = Backbone.Collection.extend({
initialize: function(){
console.log("People collection is initialized");
},
model:Person
});
var person = new Person({name:"Joao"});
var people = new People(person);
people.add([{name:"Bob"},{name:"Jim"},{name:"Nico"}]);
console.log(people.toJSON());//this shows the models that are in the collection...
//a collection is set of models (ocurrence)s - we call use ajax to populate a whole list of models
console.log("By Cid="+people.getByCid("c1"));
console.log(people.models);
// people.remove(person);
// console.log(people.toJSON());//this shows the models that are in the collection...
// people.reset([{name:"Bob"},{name:"Jim"},{name:"Nico"},person]);//redo the inicial list after add()
// console.log(people.toJSON());
// people.on("remove",function(toRemove){
// alert("You gave a command to remove "+toRemove.get("name"));
// });
// people.remove(person);
// console.log(people.toJSON());
console.log(document.title+"...... END..");
</script>
<p>Collections</p>
</body>
</html>