2011-08-20 10 views
5

¿Por qué el objeto goog.history.Html5History dispara el evento goog.history.EventType.NAVIGATE dos veces cada vez que se cambia el fragmento? Este es el ejemplo de código:Cierre de Google - Html5History activa el evento NAVIGATE dos veces

var history = goog.history.Html5History.isSupported() 
     ? new goog.history.Html5History() 
     : new goog.History(); 
goog.events.listen(history, goog.history.EventType.NAVIGATE, function(e) { 
     console.log(['navigation', e.target.getToken()]); 
}); 
history.setEnabled(true); 

Y esto es log:

["navigation", "!/properties/new"] 
["navigation", "!/properties/new"] 

UPD: Como he descubierto que hay dos valores diferentes de isNavigation campo de la e objeto de devolución de llamada. La primera vez toma el valor false, y la segunda vez toma el valor true. isNavigation significa:

isNavigation True si el evento se desencadena por una acción del navegador, tales como hacia adelante o hacia atrás, al hacer clic en un enlace, la edición de la URL, o llamando window.history (ir | espalda | adelante.) Falso si el token ha sido cambiado por una llamada setToken o replaceToken.

Pero, ¿cómo conseguir que solo uno se dispare?

Respuesta

1

Me encontré con el mismo problema. Pero en mi caso ambos eventos tienen isNavigation==true.

init = function() { 
    var h = goog.history.Html5History.isSupported() ? 
     new goog.history.Html5History() : new goog.History(); 

    goog.events.listen(h, goog.history.EventType.NAVIGATE, navigationCallback); 
    h.setEnabled(true); 
}; 

navigationCallback = function(e) { 
    console.log(e, e.token, e.isNavigation); 
}; 

// And then: 
h.setToken("example1"); 
h.setToken("example2"); 
// And click "back" button in browser 

Salida:

goog.history.Event "example1" false 
goog.history.Event "example2" false 
goog.history.Event "example1" true 
goog.history.Event "example1" true 
+1

Aquí puede encontrar simple parche: http://code.google.com/p/closure-library/issues/detail?id=449 Se eliminación Html5History unión a popstate caso navegador. –

Cuestiones relacionadas