Created
February 5, 2012 23:37
-
-
Save andrewdeandrade/1748421 to your computer and use it in GitHub Desktop.
Backbone.History
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Backbone.History.prototype.urlHistory = { | |
stack: [], | |
currentIndex: 0, | |
current: function() { | |
return this.stack[this.currentIndex]; | |
}, | |
previous: function() { | |
return this.stack[this.currentIndex - 1]; | |
}, | |
next: function() { | |
return this.stack[this.currentIndex + 1]; | |
}, | |
add: function(url) { | |
// TODO: make this compatible with the browser back button. | |
if (url === this.previous()) { | |
this.currentIndex = this.currentIndex - 1; | |
if (this.next()) { this.clearForward(); } | |
window.history.back(); | |
} else { | |
this.stack.push(url); | |
this.currentIndex = this.stack.length - 1; | |
} | |
return true; | |
}, | |
clearForward: function() { | |
this.stack = this.stack.slice(0, this.currentIndex + 1); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can you post usage example?