forked from quirkey/sammy
/
sammy.push_location_proxy.js
59 lines (53 loc) · 1.73 KB
/
sammy.push_location_proxy.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
(function($) {
Sammy = Sammy || {};
// The PushLocationProxy is an optional location proxy prototype.
// PushLocationProxy gets its location from history API. No hash needed here.
// Only compatible with Firefox >= Chrom 6, Firefox 4.0, Safari 5.0
//
// ### Example
//
// var app = $.sammy(function() {
// // set up the location proxy
// this.setLocationProxy(new Sammy.PushLocationProxy(this));
//
// this.get('/about', function() {
// // Do something here
// });
//
// });
//
// Clicking on that link would not go to /about, but would set the apps location
// to 'about' and trigger the route.
Sammy.PushLocationProxy = function(app) {
this.app = app;
};
$.extend(Sammy.PushLocationProxy.prototype , {
bind: function() {
var proxy = this;
$(window).bind('popstate', function(e) {
proxy.app.trigger('location-changed');
});
$('a').live('click', function(e) {
if (e.isDefaultPrevented()) {
return;
}
// Do not bind external links
if (location.hostname == this.hostname && proxy.app.lookupRoute('get', $(this).attr('href'))) {
e.preventDefault();
proxy.setLocation($(this).attr('href'));
proxy.app.trigger('location-changed');
}
});
},
unbind: function() {
$('a').unbind('click');
$(window).unbind('popstate');
},
getLocation: function() {
return window.location.pathname;
},
setLocation: function(new_location) {
history.pushState({ path: this.path }, '', new_location);
}
});
})(jQuery);