2011-08-20 5 views
5

Perché goog.history.Html5History object fire goog.history.EventType.NAVIGATE due volte ogni volta quando si modifica il frammento? Questo è l'esempio di codice:Google Closure - Html5History fa scattare l'evento NAVIGATE due volte

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); 

E questo è di registro:

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

UPD: Come ho capito ci sono due diversi valori di isNavigation campo della e oggetto in callback. La prima volta è necessario il valore false e la seconda volta il valore è true. isNavigation significa:

isNavigation True se l'evento è stato innescato da un'azione di browser, ad come in avanti o indietro, cliccando su un link, modifica l'URL, o chiamando window.history (go | indietro | avanti.). False se il token è stato modificato da una chiamata setToken o replaceToken.

Ma come far sparare solo uno?

risposta

1

Ho incontrato lo stesso problema. Ma nel mio caso entrambi gli eventi hanno 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 

uscita:

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

Qui trovi semplice patch: http://code.google.com/p/closure-library/issues/detail?id=449 E 'la rimozione Html5History vincolante per popstate evento browser. –