forked from joaool/MotherGithub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone1_2.html
222 lines (207 loc) · 8.23 KB
/
backbone1_2.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Backbone v1.2</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 type="text/javascript">
Book = Backbone.Model.extend({
// urlRoot: 'http://localhost:8080/books/', //use when not utilizing collections
initialize: function(){
this.on("invalid", function(model, error){
console.log("**Validation Error : " + error + "**");
});
this.on("change", function(){//detects any change
console.log('Model Changes Detected:');
if(this.hasChanged('name')){
console.log('The name has changed from ' + this.previous('name') + ' to ' + this.get('name'));
}
if(this.hasChanged('year')){
console.log('The year has changed')
}
console.log('Changed attributes: ' + JSON.stringify(this.changed));//this.changed is a json with all attributes changed
console.log('Previous attributes: ' + JSON.stringify(this.previousAttributes()));//this.previousAttributes() returns a json with all previous attributes
});
this.on("change:name", function(){
console.log('The name attribute has changed');
});
},
defaults: {
name: 'Book Title',
author: 'No One'
},
printDetails: function(){
console.log("----->"+this.get('name') + ' by ' + this.get('author'));
},
validate: function(attrs){
if(attrs.year < 2000){
return 'Year must be after 2000';
}
if(!attrs.name){
return 'A name must be provided';
}
},
parse: function(response, xhr) {
response.bookType = 'ebook';
return response;
},
});
var thisBook = new Book({name : 'Beginning Backbone', author: 'James Sugrue'});
console.log(thisBook.get('name') + ' by ' + thisBook.get('author'));
//change the value of an attribute
thisBook.set('name', 'Beginning Backbone.js');
console.log(thisBook.get('name') + ' by ' + thisBook.get('author'));
//add a new attribute (which will later be removed)
thisBook.set('year', 2013);
console.log('Book year ' + thisBook.get('year'));
//remove the attribute
thisBook.unset('year');
console.log('Book year ' + thisBook.get('year'));
//check for the existance of an attribute
var hasYear = thisBook.has('year'); //results in false
var hasName = thisBook.has('name'); //results in true
console.log('Has an attribute called year : ' + hasYear);
console.log('Has an attribute called name : ' + hasName);
//create a new object
var newBook = new Book({name: 'Another Book', author: 'You'});
newBook.clear();//remove all attributes --> this triggers the change event
console.log('Has an attribute called name : ' + newBook.has('name'));//results in false
//create a clone
var clonedBook = thisBook.clone();//a new instance of the model with identical attributes
//use the printDetails function
thisBook.printDetails();
//set the variable (expect change handler)
thisBook.set('year', 2014); //change handler invoked
thisBook.set('year', 2014, {silent:true});//no change handler invoked
//try setting the year to pre 2000
thisBook.set('year', 1999, {validate: true});
console.log('Check year change: ' + thisBook.get('year'));
thisBook.unset('name', {validate: true});
console.log('Check if name was removed ' + thisBook.get('name'));
//check if model is valid
console.log('Is model valid: ' + thisBook.isValid());
//break the validation rules by not using the validate flag
thisBook.set('year', 1998);
//check if the model is valid
console.log('Is model valid: ' + thisBook.isValid());
thisBook.set('year', 2013);
//thisBook.set('isbn', '9781430263340')
console.log('Id: ' +thisBook.get('id'));
console.log('ISBN: ' +thisBook.get('isbn'));
EBook = Book.extend({
getWebLink: function(){
return 'http://www.apress.com/'+this.get('name');
},
printDetails: function(){
console.log('An ebook');
Book.prototype.printDetails.call(this);
}
});
var ebook = new EBook({name: "Beginning Backbone", author: "James Sugrue"});
console.log(ebook.getWebLink());
console.log(ebook.printDetails());
/**
* Define a collection based on book
**/
var Library = Backbone.Collection.extend({model: Book,
url: 'http://localhost:8080/books/',
initialize: function(){
console.log('*** Creating a new library collection ***');
this.on("remove", function(removedModel, models, options){
console.log('element removed at ' + options.index);
});
this.on('reset', function(){
console.log('Reset detected');
});
},
comparator: function(a, b) {
return a.get('name') < b.get('name') ? -1 : 1;
},
parse: function(response, xhr) {
//customisations here
return response;
},
});
var bookOne = new Book({name: 'Beginning Backbone', author: 'James Sugrue', year: 2013});
var bookTwo = new Book({name: 'Pro Javascript Design Patterns', author:'Dustin Diaz, Ross Harmes', year: 2012});
var myLibrary = new Library([bookOne, bookTwo]);
console.log('Library contains ' + myLibrary.length + ' books');
var bookThree = new Book({name: 'Pro Node.js for Developers', author: 'Colin J. Ihrig', year: 2011});
myLibrary.add(bookThree);
console.log('Library contains ' + myLibrary.length + ' books');
var bookFour = new Book({name: 'Pro jQuery', author: 'Adam Freeman'});
var bookFive = new Book({name : 'Pro Javascript Performance', author: 'Tom Barker'});
myLibrary.add([bookFour, bookFive]);
console.log('**********************************************************************Library contains ' + myLibrary.length + ' books');
//--------------------------
bookOne.set('year', 2014);
bookOne.set('author', 'jojo');
// myLibrary.add(bookOne);
console.log('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Library contains ' + myLibrary.length + ' books');
console.log('Year: ' + myLibrary.get(bookOne).get('year') + " - "+'Author: ' + myLibrary.get(bookOne).get('author'));
myLibrary.remove(bookFive);
console.log('---4-----------------------Library contains ' + myLibrary.length + ' books');
myLibrary.remove([bookThree, bookFour]);
console.log('----3--2-------------------Library contains ' + myLibrary.length + ' books');
myLibrary.reset([bookOne]);
console.log('Library contains ' + myLibrary.length + ' books');
myLibrary.reset();
console.log('000000000000000000000000000000000000000000000000000000000000000000000000Library contains ' + myLibrary.length + ' books');
myLibrary = new Library([bookOne, bookTwo]);
console.log('Library contains ' + myLibrary.length + ' books');
//smart updates:
myLibrary.set([bookTwo], {remove: false});
console.log('Library contains ' + myLibrary.length + ' books');
var aBook = myLibrary.get('c5');
console.log('A book called ' + aBook.get('name'));
var anotherBook = myLibrary.at(1);
console.log('Another book is called ' + anotherBook.get('name'));
var nosuchBook = myLibrary.at(20);//results in undefined
//a simple loop
for(var i = 0; i < myLibrary.length; i++){
var model = myLibrary.at(i);
console.log('-->Book ' + i + ' is called ' + model.get('name'));
}
console.log("--------------------------------------------------------------------------------");
//RESTful operations
var myOnlineLibrary = new Library();
myOnlineLibrary.fetch({
success: function(e){
console.log('Got data. Library now has ' + myOnlineLibrary.size() + ' books');
},
error: function(e){
console.log('Something went wrong');
}
});
//add a model to the collection and save to server
myOnlineLibrary.add(bookOne);
bookOne.save({
success: function(e){
console.log('Book saved to server');
//now delete that book again
bookOne.destroy({
success: function(e){
console.log('Book deleted')
},
error: function(e){
console.log('Error deleting book');
}
});
},
error: function(e){
console.log('Error saving book');
}
});
//add to the collection and save all at once
// myOnlineLibrary.create(bookTwo);
console.log(document.title+"...... END..");
</script>
<p>Model</p>
</body>
</html>