Permalink
Browse files

Actually working pushstate after testing in an app environment

  • Loading branch information...
1 parent 65943bf commit 30d04fa8ddf5f65768550db83a524d2e505aaca7 @quirkey quirkey committed Mar 10, 2011
Showing with 22 additions and 9 deletions.
  1. +22 −9 lib/sammy.js
View
31 lib/sammy.js
@@ -221,6 +221,14 @@
this._startPolling(run_interval_every);
};
+ Sammy.DefaultLocationProxy.fullPath = function(location_obj) {
+ // Bypass the `window.location.hash` attribute. If a question mark
+ // appears in the hash IE6 will strip it and all of the following
+ // characters from `window.location.hash`.
+ var matches = location_obj.toString().match(/^[^#]*(#.+)$/);
+ var hash = matches ? matches[1] : '';
+ return [location_obj.pathname, location_obj.search, hash].join('');
+ };
Sammy.DefaultLocationProxy.prototype = {
// bind the proxy events to the current app.
@@ -237,17 +245,27 @@
app.trigger('location-changed');
});
if (_has_history) {
+ Sammy.log('has history!');
// bind to popstate
- $(window).bind('popstate.' + this.app.eventNamespace(), function() {
+ $(window).bind('popstate.' + this.app.eventNamespace(), function(e) {
+ Sammy.log('popstate', e);
app.trigger('location-changed');
});
// bind to link clicks that have routes
$('a').live('click.history-' + this.app.eventNamespace(), function(e) {
- if (this.hostname == window.location.hostname && app.lookupRoute('get', this.href)) {
+ try {
+ var full_path = Sammy.DefaultLocationProxy.fullPath(this);
+ e.preventDefault();
+ Sammy.log('click.history', this.hostname, window.location.hostname, this.pathname, 'full_path', app.lookupRoute('get', this.href));
+ if (this.hostname == window.location.hostname && app.lookupRoute('get', full_path)) {
e.preventDefault();
- proxy.setLocation(this.href);
+ proxy.setLocation(full_path);
app.trigger('location-changed');
}
+ } catch(r) {
+ console.error(r);
+ }
+ return false;
});
}
if (!Sammy.DefaultLocationProxy._bindings) {
@@ -269,12 +287,7 @@
// get the current location from the hash.
getLocation: function() {
- // Bypass the `window.location.hash` attribute. If a question mark
- // appears in the hash IE6 will strip it and all of the following
- // characters from `window.location.hash`.
- var matches = window.location.toString().match(/^[^#]*(#.+)$/);
- var hash = matches ? matches[1] : '';
- return [window.location.pathname, window.location.search, hash].join('');
+ return Sammy.DefaultLocationProxy.fullPath(window.location);
},
// set the current location to `new_location`

0 comments on commit 30d04fa

Please sign in to comment.