@@ -6,6 +6,10 @@ $(function() {
6
6
}
7
7
} ) ;
8
8
9
+ var TemplateModel = Backbone . Model . extend ( {
10
+
11
+ } ) ;
12
+
9
13
10
14
11
15
// COLLECTIONS
@@ -15,6 +19,18 @@ $(function() {
15
19
url : '/categories'
16
20
} ) ;
17
21
22
+ var TemplatesCollection = Backbone . Collection . extend ( {
23
+ model : TemplateModel ,
24
+
25
+ url : function ( ) {
26
+ return _ . result ( this . category , "url" ) + '/templates' ;
27
+ } ,
28
+
29
+ initialize : function ( options ) {
30
+ this . category = options . category ;
31
+ }
32
+ } )
33
+
18
34
19
35
20
36
// VIEWS
@@ -26,15 +42,29 @@ $(function() {
26
42
} ,
27
43
28
44
initialize : function ( ) {
29
- this . template = _ . template ( '<strong><%= category.name %></strong>' )
45
+ this . template = _ . template ( '<strong><%= category.name %></strong><ul></ul>' ) ;
30
46
31
- this . render ( ) ;
47
+ this . collection = new TemplatesCollection ( { category : this . model } ) ;
48
+
49
+ this . listenTo ( this . collection , "sync" , this . render ) ;
50
+
51
+ this . collection . fetch ( ) ;
32
52
} ,
33
53
34
54
render : function ( ) {
35
55
this . $el . html ( this . template ( {
36
56
category : this . model . toJSON ( )
37
57
} ) ) ;
58
+
59
+ this . $el . find ( "ul" ) . empty ( ) ;
60
+
61
+ var _this = this ;
62
+
63
+ this . collection . each ( function ( t ) {
64
+ var view = new TemplateView ( { model : t } ) ;
65
+
66
+ _this . $el . find ( "ul" ) . append ( view . el ) ;
67
+ } )
38
68
} ,
39
69
40
70
changeName : function ( ) {
@@ -68,6 +98,22 @@ $(function() {
68
98
}
69
99
} ) ;
70
100
101
+ var TemplateView = Backbone . View . extend ( {
102
+ tagName : "li" ,
103
+
104
+ initialize : function ( ) {
105
+ this . template = _ . template ( '<h3><%= template.name %>: </h3><p><%= template.body %></p>' ) ;
106
+
107
+ this . render ( ) ;
108
+ } ,
109
+
110
+ render : function ( ) {
111
+ this . $el . html ( this . template ( {
112
+ template : this . model . toJSON ( )
113
+ } ) ) ;
114
+ }
115
+ } ) ;
116
+
71
117
var view = new CategoriesView ( ) ;
72
118
73
119
$ ( "#app-container" ) . html ( view . el ) ;
0 commit comments