Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backbone-beforepopstate Breaks Backbone's "page not found (404)" Functionality #3

Open
machineghost opened this issue Oct 1, 2013 · 0 comments

Comments

@machineghost
Copy link

Backbone-beforepopstate overrides the native Backbone.history.start with its own version. Now the new version calls the original, so everything continues to work as normal, except for one thing: the new version doesn't return the value the original version would.

Normally Backbone.history.start returns either true or false, depending on whether it matched a route or not (ie. whether it got a pseudo-404). However, beforepopstate does the following:

BB.history._originalStart(options);

without storing or returning the return value.

One simple solution would be to save the return value, and then return it after the on('popstate' section:

var matchFound = BB.history._originalStart(options);

// This prevents the popstate event handler from calling any handlers after
// the one that backbone uses to fire navigation
jQuery(window).on('popstate', function(e) {
  if (history._popCancelled) {
    e.stopImmediatePropagation();
    e.stopPropagation();
    e.preventDefault();
    history._popCancelled = false;
  }
});
return matchFound;

However, if the hookup of the popstate event handler could happen before start is called, then all that cold be simplified to:

jQuery(window).on('popstate', function(e) {
...
});
return BB.history._originalStart(options);

Either way, please preserve the return value of start, as it's an important part of Backbone routing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant