-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
144 lines (127 loc) · 4.33 KB
/
main.js
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
$(function(){
$('body').ajaxError(function(event, request, settings, err){
console.log(event);
});
$.ajaxSetup({
cache: false
});
var blog = {};
blog.views = {};
blog.helper = {};
blog.helper.build_main_model = function(data){
var result = {};
result.site_name = data.site_name;
result.copyright = data.copyright;
result.navlist = _.map(data.cates, function(cate){
return {link: '#!cate/'+ cate.name, text: cate.text};
});
return result;
};
blog.helper.build_sidebar_model = function(data, cate){
var result = {};
var articles = data.articles;
if(cate){
articles = _.filter(articles, function(article){return article.cate == cate;});
}
result.months = _.groupBy(articles, function(article){
return article.file.substring(0, 7);
});
result.months = _.map(result.months, function(value, key){
return {month: key,
articles: _.map(value, function(article){
return {link: article.file, text: article.title}
})
};
});
return result;
};
blog.helper.markdown = new Showdown.converter();
blog.views.Sidebar = Backbone.View.extend({
template: $('#tpl-sidebar').html(),
initialize: function(options){
this.model = options.model;
_.bindAll(this, 'render');
},
render: function(){
var html = Mustache.to_html(this.template, this.model);
$(this.el).append(html);
return this;
}
});
blog.views.Article = Backbone.View.extend({
initialize: function(options){
var that = this;
this.article = options.article;
_.bindAll(this, 'render');
$.get('post/'+this.article+'.txt', function(data){
that.model = data;
that.render();
});
},
render: function(){
if(!this.model) return this;
var html = blog.helper.markdown.makeHtml(this.model);
$(this.el).html(html);
}
});
blog.views.Main = Backbone.View.extend({
el:$('.main-body'),
template: $('#tpl-main').html(),
initialize: function(){
_.bindAll(this, 'render');
_.bindAll(this, 'sync');
},
sync: function(){
var that = this;
$.getJSON('meta.js', function(data){
that.data = data;
that.render();
});
},
render: function(){
if(!this.data){
this.sync();
return this;
}
var main_model = blog.helper.build_main_model(this.data);
var main_html = Mustache.to_html(this.template, main_model);
$(this.el).empty().append(main_html);
var sidebar_mode = blog.helper.build_sidebar_model(this.data, this.cate);
var sidebar_view = new blog.views.Sidebar({model: sidebar_mode});
this.$(".sidebar-nav").empty().append(sidebar_view.render().el);
if(this.cate){
this.$('.navbar-inner .nav li a[href="#!cate/'+this.cate+'"]').parent().addClass('active');
}
if(this.article){
var article_view = new blog.views.Article({article:this.article});
this.$(".article-content").empty().append(article_view.render().el);
}
}
});
blog.App = Backbone.Router.extend({
routes: {
"" : "index",
"!cate/:cate" : "cate",
"!show/:article" : "show"
},
make_main_view: function(cate, article){
if(!this.main){
this.main = new blog.views.Main();
}
this.main.cate = cate;
this.main.article = article;
this.main.render();
},
index: function(){
this.make_main_view(null, 'index');
},
cate: function(cate){
this.make_main_view(cate, 'index');
},
show: function(article){
this.make_main_view(null, article);
}
});
app = new blog.App();
Backbone.history.start();
});