-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
110 lines (94 loc) · 3.29 KB
/
index.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<!DOCTYPE html>
<html>
<head>
<title>I have a back bone</title>
</head>
<body>
<button id="add-friend">Add Friend</button>
<ul id="friends-list">
</ul>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.4/underscore-min.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"></script>
<script>
(function ($) {
Friend = Backbone.Model.extend({
//Create a model to hold friend atribute
name: null,
lastname: null,
child: null,
// Default values
defaults: {
name: 'Alguna',
lastname: 'Persona',
child: 'El hijo de alguna persona'
},
initialize: function() {
console.log("Inicializando Modelo");
// cambié this.on a this.bind, no reconoce .on
this.bind("change:name", function(model) {
var name = model.get("name");
console.log("Cambié mi nombre a " + name);
});
this.bind("change", function(model){
console.log("Algo cambió en el modelo.")
});
},
// custom method adopt
adopt: function(friendChild) {
this.set({child: friendChild});
}
});
Friends = Backbone.Collection.extend({
//This is our Friends collection and holds our Friend models
initialize: function (models, options) {
console.log("Inicializando Colección");
this.bind("add", options.view.addFriendLi);
//Listen for new additions to the collection and call a view function if so
}
});
AppView = Backbone.View.extend({
el: $("body"),
initialize: function () {
console.log("Inicializando Vista");
this.friends = new Friends( null, { view: this });
var friend1 = new Friend({name: "Mauro", lastname: "Carrero"});
var friend2 = new Friend({name: "Virginia", lastname: "Aniano"});
var friend3 = new Friend({name: "Lucas", lastname: "Dominguez"});
var friend4 = new Friend({name: "Ignacio", lastname: "Carrero"});
this.friends.add(friend1);
this.friends.add(friend2);
this.friends.add(friend3);
this.friends.add(friend4);
this.friends.add(new Friend());
this.friends.add(new Friend());
this.friends.add(new Friend());
this.friends.add(new Friend());
friend4.set({name: "Nacho"});
// custom model method
friend1.adopt(friend4);
console.log("El hijo de " + friend1.get('name') + " es " + friend1.get('child').get('name'));
//Create a friends collection when the view is initialized.
//Pass it a reference to this view to create a connection between the two
},
events: {
"click #add-friend": "showPrompt",
},
showPrompt: function () {
var friendName = prompt("Who is your friend?");
var friendLastName = prompt("What is his/her lastname?");
var friendModel = new Friend({ name: friendName, lastname: friendLastName });
//Add a new friend model to our friend collection
this.friends.add( friendModel );
},
addFriendLi: function (model) {
//The parameter passed is a reference to the model that was added
$("#friends-list").append("<li>" + model.get('name') + " " + model.get('lastname') + "</li>");
//Use .get to receive attributes of the model
}
});
var appview = new AppView;
})(jQuery);
</script>
</body>
</html>