Skip to content

Commit 90240b4

Browse files
author
Andy Creeth
committed
step 8
1 parent 710ceb9 commit 90240b4

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

public/app.js

+48-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ $(function() {
66
}
77
});
88

9+
var TemplateModel = Backbone.Model.extend({
10+
11+
});
12+
913

1014

1115
// COLLECTIONS
@@ -15,6 +19,18 @@ $(function() {
1519
url: '/categories'
1620
});
1721

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+
1834

1935

2036
// VIEWS
@@ -26,15 +42,29 @@ $(function() {
2642
},
2743

2844
initialize: function() {
29-
this.template = _.template('<strong><%= category.name %></strong>')
45+
this.template = _.template('<strong><%= category.name %></strong><ul></ul>');
3046

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();
3252
},
3353

3454
render: function() {
3555
this.$el.html(this.template({
3656
category: this.model.toJSON()
3757
}));
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+
})
3868
},
3969

4070
changeName: function() {
@@ -68,6 +98,22 @@ $(function() {
6898
}
6999
});
70100

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+
71117
var view = new CategoriesView();
72118

73119
$("#app-container").html(view.el);

0 commit comments

Comments
 (0)