Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Closure - Html5History fires NAVIGATE event twice

Why does goog.history.Html5History object fire goog.history.EventType.NAVIGATE event twice each time when fragment is changed? This is the example of code:

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

And this is log:

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

UPD: As I have figured out there are two different values of isNavigation field of e object in callback. First time it takes false value, and second time it takes true value. isNavigation means:

isNavigation True if the event was triggered by a browser action, such as forward or back, clicking on a link, editing the URL, or calling window.history.(go|back|forward). False if the token has been changed by a setToken or replaceToken call.

But how to get only one even fired?

like image 274
Eugene Manuilov Avatar asked Nov 14 '22 18:11

Eugene Manuilov


1 Answers

I met the same problem. But in my case both events has 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

Output:

goog.history.Event "example1" false
goog.history.Event "example2" false
goog.history.Event "example1" true
goog.history.Event "example1" true
like image 172
Paweł Szczur Avatar answered Dec 15 '22 07:12

Paweł Szczur