multiple matching routes

后端 未结 5 1193
广开言路
广开言路 2021-01-31 06:22

I\'ve got a backbone.js application that defines two controllers, and the controllers both define route patterns which match the location.hash. I\'m having trouble getting both

5条回答
  •  伪装坚强ぢ
    2021-01-31 06:46

    I haven't fully tested this yet, if you take a look at the Backbone.js source, you can see this at line 1449:

    // Attempt to load the current URL fragment. If a route succeeds with a
    // match, returns `true`. If no defined routes matches the fragment,
    // returns `false`.
    loadUrl: function(fragment) {
      fragment = this.fragment = this.getFragment(fragment);
      return _.any(this.handlers, function(handler) {
        if (handler.route.test(fragment)) {
          handler.callback(fragment);
          return true;
        }
      });
    }
    

    The any method will stop as soon as it matches a handler route (with the "return true"), just comment the return and the short-circuit will never happend, and all the handlers will be tested. Tested this with a marionette app with two modules, each one having it's own router and controller, listening same routes anb both fired up.

提交回复
热议问题