Skip to content

Commit

Permalink
Actually working pushstate after testing in an app environment
Browse files Browse the repository at this point in the history
  • Loading branch information
quirkey committed Mar 10, 2011
1 parent 65943bf commit 30d04fa
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions lib/sammy.js
Expand Up @@ -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.
Expand All @@ -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) {
Expand All @@ -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`
Expand Down

0 comments on commit 30d04fa

Please sign in to comment.