-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfront.js
More file actions
59 lines (45 loc) · 1.28 KB
/
front.js
File metadata and controls
59 lines (45 loc) · 1.28 KB
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
// Front - Our own frontend framework
Front = {}
Front.navigate = function(path) {
window.history.pushState({}, "ignored", path)
Front.load()
}
Front.start = function() {
$(window).on('popstate', Front.load)
// Execute the route for the current location
Front.load()
}
Front.routes = []
Front.route = function(path, callback, context) {
path = path.replace(/:\w+/g, '([^/?]+)') // Replace named params (eg. :permalink)
var regexp = new RegExp('^' + path + '$')
this.routes.push({
regexp: regexp,
callback: callback,
context: context
})
}
Front.load = function() {
var url = window.location.pathname
for (var i = 0; i < Front.routes.length; i++) {
var route = Front.routes[i]
var matches = url.match(route.regexp)
if (matches) {
route.callback.apply(route.context, matches.slice(1))
return
}
}
}
// A nicer API
Front.Router = function(routes) {
for (var path in routes) {
var callback = routes[path]
Front.route(path, callback, this)
}
}
Front.Router.prototype.render = function(template, data) {
var html = $("[data-template-name='" + template + "']").html()
// TODO cache this! We don't want to compile the template each time.
var compiled = Handlebars.compile(html)
$("#content").html(compiled(data))
}