-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmvcBlog.html
309 lines (299 loc) · 8.39 KB
/
mvcBlog.html
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>前端MVC</title>
<style>
li {
list-style: none;
margin: 5px 0;
}
fieldset {
margin: 5px 0;
}
</style>
<script src="js/lib/zepto.js" type="text/javascript"></script>
<script src="js/lib/underscore.js" type="text/javascript"></script>
<script src='js/utils/underscore.extend.js'></script>
</head>
<body>
<div id="main">
<fieldset>
<legend>文章总数</legend>
<div class="js_num">
0
</div>
</fieldset>
<fieldset>
<legend>分类</legend>
<div class="js_type_wrapper">
</div>
</fieldset>
<fieldset>
<legend>标签</legend>
<div class="js_label_wrapper">
</div>
</fieldset>
<fieldset>
<legend>博客列表</legend>
<div class="js_blogs_wrapper">
</div>
</fieldset>
<fieldset>
<legend>新增博客</legend>
<ul>
<li>标题 </li>
<li>
<input type="text" class="js_title" />
</li>
<li>类型 </li>
<li>
<input type="text" class="js_type" />
</li>
<li>标签(逗号隔开) </li>
<li>
<input type="text" class="js_label" />
</li>
<li>
<input type="button" class="js_add" value="新增博客" />
</li>
</ul>
</fieldset>
</div>
<script type="text/template" id="js_tpl_kv">
<ul>
<%for(var k in objs){ %>
<li><%=k %>(<%=objs[k] %>)</li>
<%} %>
</ul>
</script>
<script type="text/template" id="js_tpl_blogs">
<ul>
<%for(var i = 0, len = blogs.length; i < len; i++ ){ %>
<li><%=blogs[i].title %> - <span class="js_blog_del" data-id="<%=blogs[i].id %>">删除</span></li>
<%} %>
</ul>
</script>
<!-- <script src="js/require.js" data-main="js/main"></script> -->
<script>
//基类view
var AbstractView = _.inherit({
initialize: function () {
this.propertys()
},
propertys: function () {
this.$el = $('#main')
this.events = {};
},
$: function (selector) {
return this.$el.find(selector)
},
show: function () {
this.$el.show()
this.bindEvents()
},
bindEvents: function () {
var events = this.events
if (!(events || (events = _.result(this, 'events')))) return this
this.unBindEvents()
var delegateEventSplitter = /^(\S+)\s*(.*)$/
var k, method, match, eventName, selector
for (k in events) {
method = events[k]
if (!_.isFunction(method)) method = this[events[k]]
if (!method) continue
match = k.match(delegateEventSplitter)
eventName = match[1], selector = match[2]
method = _.bind(method, this)
eventName += '.delegateUIEvents' + this.id
if (selector == '')
this.$el.on(eventName, method)
else
this.$el.on(eventName, selector, method)
}
return this
},
unBindEvents: function () {
this.$el.off('.delegateUIEvents' + this.id)
return this
}
})
//基类Model
var AbstractModel = _.inherit({
initialize: function (opts) {
this.propertys()
this.setOption(opts)
},
propertys: function () {
this.data = {}
this.controllers = {}
this.initialController = null
this.scope = null
},
setOption: function (opts) {
for (var k in opts) {
this[k] = opts[k]
}
},
addController: function (k, v) {
if (!k || !v) return
this.controllers[k] = v
},
removeController: function (k) {
if (!k) return
delete this.controllers[k]
},
initData: function (data) {
var k
if (!data) return
for (k in this.data) {
if (data[k]) this.data[k] = data[k]
}
this.handleData()
this.initialController.call(this.scope, this.get())
},
handleData: function () { },
get: function () {
if (!this.validateData()) return {}
return this.formatData(this.data)
},
validateData: function () { return true },
formatData: function (data) { return data },
update: function (k) {
var data = this.get(), i
if (!data) return
if (this.controllers[k]) {
this.do(k, data)
return
}
for (i in this.controllers)
this.do(i, data)
},
do: function (k, data) {
if (typeof this.controllers[k] === 'function')
this.controllers[k].call(this.scope, data)
else if (typeof this.controllers[k].render === 'function')
this.controllers[key].render.call(this.scope, data);
}
})
//博客的model模块
var Model = _.inherit(AbstractModel, {
propertys: function () {
this.data = {
blogs: []
}
},
add: function (title, type, label) {
if (!title || !type) return null
var blog = {}
blog.id = 'blog_' + _.uniqueId()
blog.title = title
blog.type = type
if (label) blog.label = label.split(',')
else blog.label = []
this.data.blogs.push(blog)
this.update()
return blog
},
remove: function (id) {
if (!id) return null
var i, len, data
for (i = 0, len = this.data.blogs.length; i < len; i++) {
if (this.data.blogs[i].id === id) {
data = this.data.blogs.splice(i, 1)
this.update()
return data
}
}
return null
},
getTypeInfo: function () {
var o = {}, i, len, type
for (i = 0, len = this.data.blogs.length; i < len; i++) {
type = this.data.blogs[i].type
if (!o[type]) o[type] = 1
else o[type] = o[type] + 1
}
return o
},
getLabelInfo: function () {
var o = {}, i, j, len, len1, blog, label
for (i = 0, len = this.data.blogs.length; i < len; i++) {
blog = this.data.blogs[i]
for (j = 0, len1 = blog.label.length; j < len1; j++) {
label = blog.label[j]
if (!o[label]) o[label] = 1
else o[label] = o[label] + 1
}
}
return o
},
getNum: function () {
return this.data.blogs.length
}
})
//页面主流程
var View = _.inherit(AbstractView, {
propertys: function ($super) {
$super()
this.$el = $('#main')
this.events = {
'click .js_add': 'blogAddAction',
'click .js_blog_del': 'blogDeleteAction'
}
this.model = new Model({
scope: this,
controllers: {
numController: this.numController,
typeController: this.typeController,
labelController: this.labelController,
blogsController: this.blogsController
}
})
},
numController: function () {
this.$('.js_num').html(this.model.getNum())
},
typeController: function () {
var html = ''
var tpl = document.getElementById('js_tpl_kv').innerHTML
var data = this.model.getTypeInfo()
html = _.template(tpl)({ objs: data })
this.$('.js_type_wrapper').html(html);
},
labelController: function () {
var html = ''
var tpl = document.getElementById('js_tpl_kv').innerHTML
var data = this.model.getLabelInfo()
html = _.template(tpl)({ objs: data })
this.$('.js_label_wrapper').html(html)
},
blogsController: function () {
var html = ''
var tpl = document.getElementById('js_tpl_blogs').innerHTML
var data = this.model.get()
html = _.template(tpl)(data)
this.$('.js_blogs_wrapper').html(html)
},
blogAddAction: function () {
//validate
this.model.add(
this.$('.js_title').val(),
this.$('.js_type').val(),
this.$('.js_label').val()
)
},
blogDeleteAction: function (e) {
var el = $(e.currentTarget)
this.model.remove(el.attr('data-id'))
}
})
var view = new View()
view.show()
</script>
</body>
</html>