|
1 | 1 | (function() {
|
2 |
| - /* |
3 |
| - The goal of this file is to provide the basic understanding |
4 |
| - 1. Create a Collection class. |
5 |
| - 2. Passing Model to Collection. |
6 |
| - 3. Setting build-in Collection events. |
7 |
| - 4. Get Model using at-index. |
8 |
| - 5. Remove Model from the Collection. |
9 |
| - 6. Get the length of Collection. |
10 |
| - 7. Add Model into the Collection at the end. |
11 |
| - 8. Remove Model from the Collection from the end. |
12 |
| - 9. Add Model into the Collection at the start. |
13 |
| - 10. Remove Model from the Collection from the start. |
14 |
| - 11. Getting data in object & string format. |
15 |
| -
|
16 |
| -
|
17 |
| - How to run this example. |
18 |
| - 1. Open Example-1.html in Google Chrome browser. |
19 |
| - 2. Press F12, go to console tab. |
20 |
| - 3. See the message get displayed on that console tab. |
21 |
| - */ |
| 2 | + /** |
| 3 | + * The goal of this file is to provide the basic understanding of |
| 4 | + * 1. Create a Collection |
| 5 | + * 2. Passing Model to Collection |
| 6 | + * 3. Setting build-in Collection events |
| 7 | + * 4. Add/Remove Model from collection at the end |
| 8 | + * 5. Add/Remove Model from collection at the start |
| 9 | + * 6. Get the length of collection |
| 10 | + */ |
22 | 11 |
|
23 | 12 | var MasterModel = Backbone.Model.extend({
|
24 |
| - /* |
25 |
| - This initialize function will get called when the model is first created. |
26 |
| - */ |
| 13 | + /** |
| 14 | + * `initialize` is a constructor function which gets called automatically at |
| 15 | + * the time of instance creation |
| 16 | + */ |
27 | 17 | initialize: function() {
|
28 | 18 | console.log("Your model have been initialize");
|
29 | 19 | }
|
30 | 20 | });
|
31 | 21 |
|
32 |
| - /* |
33 |
| - Creating a new collection called MasterCollection by extending Backbone.Collection class. |
34 |
| - Syntax: Backbone.Collection.extend(properties, [classProperties]) |
35 |
| - */ |
36 | 22 | var MasterCollection = Backbone.Collection.extend({
|
37 |
| - /* |
38 |
| - This initialize function will get called when the collection is first created. |
39 |
| - */ |
40 | 23 | initialize: function() {
|
41 | 24 | console.log("Your collection have been initialize.");
|
| 25 | + |
| 26 | + /** |
| 27 | + * Subscribe collection events |
| 28 | + */ |
42 | 29 | this.listenTo(this, 'add', this.onModelAdded);
|
43 |
| - this.listenTo(this, 'remove', this.onModelRemoved); |
| 30 | + this.listenTo(this, 'remove', this.onModelRemoved); |
44 | 31 | },
|
45 | 32 |
|
46 |
| - /* |
47 |
| - Used to specify the model class that the collection contains. |
48 |
| - */ |
| 33 | + /** |
| 34 | + * Used to specify the model class that the collection contains. |
| 35 | + */ |
49 | 36 | model: MasterModel,
|
50 | 37 |
|
51 | 38 | onModelAdded: function() {
|
52 |
| - console.log("[Add model event got fired]"); |
| 39 | + console.log("Add model event got fired"); |
53 | 40 | },
|
54 | 41 |
|
55 | 42 | onModelRemoved: function() {
|
56 |
| - console.log("[Remove model event got fired]"); |
| 43 | + console.log("Remove model event got fired"); |
57 | 44 | }
|
58 | 45 | });
|
59 | 46 |
|
60 |
| - /* |
61 |
| - Creating an object from MasterCollection which calls initialize function. |
62 |
| - */ |
63 | 47 | var masterCollection = new MasterCollection();
|
64 | 48 |
|
65 |
| - /* |
66 |
| - Add a model (or an array of models) to the collection. |
67 |
| - Firing an "add" event. |
68 |
| - */ |
69 |
| - masterCollection.add(new MasterModel({name: 'Ashwin Hegde'})); |
70 |
| - masterCollection.add(new MasterModel({name: 'Vinayak Patil'})); |
71 |
| - masterCollection.add(new MasterModel({name: 'Jerin John'})); |
72 |
| - |
73 |
| - /* |
74 |
| - Display the data currently added in the collection. |
75 |
| - */ |
| 49 | + /** |
| 50 | + * Add a model (or an array of models) to the collection. |
| 51 | + * Firing an "add" event. |
| 52 | + */ |
| 53 | + masterCollection.add(new MasterModel({ |
| 54 | + name: 'Ashwin Hegde' |
| 55 | + })); |
| 56 | + masterCollection.add(new MasterModel({ |
| 57 | + name: 'Vinayak Patil' |
| 58 | + })); |
| 59 | + masterCollection.add(new MasterModel({ |
| 60 | + name: 'Pavan Solanki' |
| 61 | + })); |
| 62 | + |
| 63 | + /** |
| 64 | + * Display the data currently added in the collection. |
| 65 | + */ |
76 | 66 | console.log("Data in Collection :: Add ")
|
77 | 67 | console.log(masterCollection.toJSON());
|
78 | 68 |
|
79 |
| - /* |
80 |
| - Remove a model (or an array of models) from the collection. Fires a "remove" event, |
81 |
| -
|
82 |
| - `at` is used to get a model from a collection, specified by index. |
83 |
| - Note: index starts from 0 |
84 |
| - */ |
| 69 | + /** |
| 70 | + * Remove a model (or an array of models) from the collection. Fires a "remove" event, |
| 71 | + * |
| 72 | + * `at` is used to get a model from a collection, specified by index. |
| 73 | + * Note: index starts from 0 |
| 74 | + */ |
85 | 75 | masterCollection.remove(masterCollection.at(1));
|
86 |
| - /* |
87 |
| - Display the data after removing model from the collection. |
88 |
| - */ |
| 76 | + |
| 77 | + /** |
| 78 | + * Display the data after removing model from the collection. |
| 79 | + */ |
89 | 80 | console.log("Remove data from Collection :: Remove ")
|
90 | 81 | console.log(masterCollection.toJSON());
|
91 | 82 |
|
92 |
| - /* |
93 |
| - Collection maintains a length property, counting the number of models it contains. |
94 |
| - */ |
| 83 | + /** |
| 84 | + * Collection maintains a length property, counting the number of models it contains. |
| 85 | + */ |
95 | 86 | console.log("Collection length: " + masterCollection.length);
|
96 | 87 |
|
| 88 | + /** |
| 89 | + * Add a model at the end of a collection. |
| 90 | + */ |
| 91 | + masterCollection.push(new MasterModel({ |
| 92 | + name: 'Kumar Kundan' |
| 93 | + })); |
97 | 94 |
|
98 |
| - /* |
99 |
| - Add a model at the end of a collection. |
100 |
| - */ |
101 |
| - masterCollection.push(new MasterModel({name: 'Saju Sasidharan'})); |
102 |
| - /* |
103 |
| - Display the data currently added in the collection. |
104 |
| - */ |
| 95 | + /** |
| 96 | + * Display the data currently added in the collection. |
| 97 | + */ |
105 | 98 | console.log("Data in Collection :: Push");
|
106 | 99 | console.log(masterCollection.toJSON());
|
107 | 100 |
|
108 |
| - /* |
109 |
| - Remove and return the last model from a collection |
110 |
| - */ |
| 101 | + /** |
| 102 | + * Remove and return the last model from a collection |
| 103 | + */ |
111 | 104 | masterCollection.pop();
|
112 |
| - /* |
113 |
| - Display the data after removing last model from the collection. |
114 |
| - */ |
| 105 | + |
| 106 | + /** |
| 107 | + * Display the data after removing last model from the collection. |
| 108 | + */ |
115 | 109 | console.log("Data in Collection :: POP");
|
116 | 110 | console.log(masterCollection.toJSON());
|
117 | 111 |
|
| 112 | + /** |
| 113 | + * Add a model at the beginning of a collection. |
| 114 | + */ |
| 115 | + masterCollection.unshift(new MasterModel({ |
| 116 | + name: 'Ajay Sajwan' |
| 117 | + })); |
118 | 118 |
|
119 |
| - /* |
120 |
| - Add a model at the beginning of a collection. |
121 |
| - */ |
122 |
| - masterCollection.unshift(new MasterModel({name: 'Yogesh Gaikwad'})); |
123 |
| - /* |
124 |
| - Display the data currently added in the collection. |
125 |
| - */ |
| 119 | + /** |
| 120 | + * Display the data currently added in the collection. |
| 121 | + */ |
126 | 122 | console.log("Data in Collection :: unshift");
|
127 | 123 | console.log(masterCollection.toJSON());
|
128 | 124 |
|
129 |
| - /* |
130 |
| - Remove and return the first model from a collection. |
131 |
| - */ |
| 125 | + /** |
| 126 | + * Remove and return the first model from a collection. |
| 127 | + */ |
132 | 128 | masterCollection.shift();
|
133 |
| - /* |
134 |
| - Display the data after removing first model from the collection. |
135 |
| - */ |
| 129 | + |
| 130 | + /** |
| 131 | + * Display the data after removing first model from the collection. |
| 132 | + */ |
136 | 133 | console.log("Data in Collection :: Shift");
|
137 | 134 | console.log(masterCollection.toJSON());
|
138 | 135 |
|
|
0 commit comments