¿Por qué tengo que presionar atrás dos veces para volver a Backbone?
Frecuentes
Visto 1,418 veces
0
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?
HTML
<div id="home">Home</div>
<div id="page1">Page1</div>
<div id="page2">Page2</div>
<div id="back">Back</div>
JavaScript
var app = {};
app.router = Backbone.Router.extend({
routes: {
'': 'home',
'page1': 'page1',
'page2': 'page2',
},
home: function () {
console.log('home');
Backbone.history.history.pushState('search');
console.log(Backbone.history.history);
},
page1: function () {
console.log('page1');
Backbone.history.history.pushState('page1');
console.log(Backbone.history.history);
},
page2: function () {
console.log('page2');
Backbone.history.history.pushState('page2');
console.log(Backbone.history.history);
},
});
var router = new app.router();
Backbone.history.start();
$(document).on('click', '#back', function () {
console.log('back');
console.log(Backbone.history.history);
Backbone.history.history.back();
console.log(Backbone.history.history);
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});
});
1 Respuestas
0
Does this serve your purpose :
$(document).on('click', '#back', function () {
window.history.back();
});
DND ventana.historia
Respondido el 02 de diciembre de 13 a las 19:12
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas javascript backbone.js url-routing backbone-routing html5-history or haz tu propia pregunta.
I don't know Backbone, but I would guess that it calls
pushState
for you, so you shouldn't call it yourself. - SLaks@SLaks If I remove that line then it takes me back to the main page. Without calling
pushState()
Backbone.history.history.state
seríaundefined
. - totymedliNot sure why you're adding pushState calls yourself into the application. Why not allow navigate to do it for you? - kinakuta
@kinakuta Because without
pushState()
It brings me back to the front page or the previous website. - totymedliAgain, 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. - kinakuta