¿Por qué tengo que presionar atrás dos veces para volver a Backbone?

Live Demo of the problem

I have this little example app, where I try to implement a routing system with history. It works as excepted, but for some reason I have to press the back button twice. If you open the console, you can see the logs. When I press for example page1 and page2 and then the back link too, it logs page2 twice. Why? If I press it again it logs page1 twice. What? Then it finally goes back. Why do I have to press it twice? Why the logs are so crazy? How can I make it work?


<div id="home">Home</div>
<div id="page1">Page1</div>
<div id="page2">Page2</div>
<div id="back">Back</div>


var app = {};
app.router = Backbone.Router.extend({

    routes: {
        '': 'home',
        'page1': 'page1',
        'page2': 'page2',

    home: function () {

    page1: function () {

    page2: function () {


var router = new app.router();

$(document).on('click', '#back', function () {
    router.navigate(Backbone.history.history.state, {trigger: true, replace: true});

$(document).on('click', '#home', function () {
    router.navigate('home', {trigger: true, replace: true});

$(document).on('click', '#page1', function () {
    router.navigate('page1', {trigger: true, replace: true});

$(document).on('click', '#page2', function () {
    router.navigate('page2', {trigger: true, replace: true});

preguntado el 02 de diciembre de 13 a las 16:12

I don't know Backbone, but I would guess that it calls pushState for you, so you shouldn't call it yourself. -

@SLaks If I remove that line then it takes me back to the main page. Without calling pushState() Backbone.history.history.state sería undefined. -

Not sure why you're adding pushState calls yourself into the application. Why not allow navigate to do it for you? -

@kinakuta Because without pushState() It brings me back to the front page or the previous website. -

Again, why not just use navigate to create your history entries? In general, I'd avoid using navigate to trigger navigation and instead use it to simply manage your browser history. -

1 Respuestas

Does this serve your purpose :

$(document).on('click', '#back', function () {

DND ventana.historia

Respondido el 02 de diciembre de 13 a las 19:12

Did you tried your solution? ejemplo 1: If I copy this function, and delete the pushState() it brings me back to the last page. ejemplo 2: If I copy this function and keep the pushState() than I have to press back twice again. - totymedli

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.