2

I'm using devise, rails with backbone. All my backbone routes are working just fine. But non-backbone routes like /accounts/login that are supposed to rendered by rails are being globbed with backbone router.

SS.Routers.ApplicationRouter = Backbone.Router.extend({
  initialize: function() {
    this.el = $("#content");
  },

  routes: {
    "": "home"
  },

  "home": function () {
    console.debug("Got home request");
    var view = new SS.Views.Home();
    this.el.empty().append(view.render());
  }
});

The actual request/response to the /accounts/login is happening from rails logs. But Backbone home root gets triggered afterwards and my home page is rendered.

My layout has

  $(function () {
    SS.init();
  });

from

window.SS = {
  Models: {},
  Collections: {},
  Views: {Providers: {}},
  Routers: {},
  init: function (data) {
    console.debug("Initializing Backbone Components");
    new SS.Routers.ApplicationRouter();
    new SS.Routers.ProvidersRouter();
    if (!Backbone.history.started) {
      Backbone.history.start();
      Backbone.history.started = true;
    };
  }
};

Which is triggering my home route again.

"" route in backbone is not supposed to be globbing /accounts/login but it is.

A little bit of debugging is showing me that /accounts/login is being gobbled by "" since the fragment is an empty string.

And the fragment is an empty string in the all the cases where there is no match for backbone routes.

Code from backbone 0.9.2

loadUrl: function(fragmentOverride) {
      var fragment = this.fragment = this.getFragment(fragmentOverride);
      var matched = _.any(this.handlers, function(handler) {
        if (handler.route.test(fragment)) {
          console.debug(handler);
          console.debug(fragment);
          handler.callback(fragment);
          return true;
        }
      });
      return matched;
    },

Any suggestions?

user689541
  • 21
  • 2

1 Answers1

0

Add a class ('passThrough') or 'data-passThrough=true' attribute to the link. Catch this class/attribute in your router and return true so Backbone stops handling it and the browser treats it as a regular link.

mcrider
  • 391
  • 3
  • 3