What is a collection?

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
 3 <head>
 4     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
 5     <title></title>
 6     <script type="text/javascript" src="jquery-1.8.0.min.js"></script>
 7     <script type="text/javascript" src="underscore.js"></script>
 8     <script type="text/javascript" src="backbone.js"></script>
 9     <script type="text/javascript" src="index.js"></script>
10 </head>
11 <body>
12     
13 </body>
14 </html>

 

 1 var Song = Backbone.Model.extend({
 2     defaults: {
 3         name: "Not specified",
 4         artist: "Not specified"
 5     },
 6     initialize: function() {
 7         console.log('Music is the answer');
 8     }
 9 });
10 
11 var Album = Backbone.Collection.extend({
12     model: Song
13 });
14 
15 var song1 = new Song({
16     name: "How Bizarre",
17     artist: "OMC"
18 });
19 var song2 = new Song({
20     name: "Sexual Healing",
21     artist: "Marvin Gaye"
22 });
23 var song3 = new Song({
24     name: "Talk It Over In Bed",
25     artist: "OMC"
26 });
27 
28 var myAlbum = new Album([song1, song2, song3]);
29 console.log(myAlbum.models);
30 console.log(JSON.stringify(myAlbum.models[0]));
31 
32 var name1 = song1.get("name");
33 console.log(name1);
34 var artist1 = song1.get("artist");
35 console.log(artist1);
posted @ 2012-08-17 11:39  小猩猩君  阅读(210)  评论(0编辑  收藏  举报