multiple matching routes

后端 未结 5 1194
广开言路
广开言路 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:41

    I think this is the simplest way of resolving it

    routes: { '': 'userGrid', 'users': 'userGrid', }

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-31 06:48

    I have a very similar issue. At present, backbone stops after the first matching route. I have a dirty workaround where I am overriding the loadUrl method of Backbone History. Here I am iterating through all of the registered routes and triggering callback for all of the matching routes .

    _.extend(Backbone.History.prototype, {
      loadUrl : function() {
        var fragment = this.fragment = this.getFragment();
        var matched = false;
        _.each(this.handlers, function(handler) {
          if (handler.route.test(fragment)) {
            handler.callback(fragment);
            matched = true;
          }
        });
        return matched;
      }
    })
    

    Philosophically, I am fine with having single controller per page. However, in a component based view framework, it will be nice to have multiple views per route rendering different parts of a view state.

    Comments are welcome.

    0 讨论(0)
  • 2021-01-31 06:49

    Short answer: No, you can't do that. One Controller per page.

    Long answer: When you instantiate a new Controller, it adds its routes to the History singleton. The History singleton is monitoring the hash component of the URL, and when the hash changes, it scans the routes for the first expression that matches its needs. It then fires the function associated with that route (that function has been bound to the controller in which it was declared). It will only fire once, and if there is a conflict the order in which it fires is formally indeterminate. (In practice it's probably deterministic.)

    Philosophical answer: The controller is a "view" object which affects the presentation of the whole page based on the hash component of the URL. Its purpose is to provide bookmark-capable URLs that the user can reach in the future, so that when he goes to a URL he can start from a pre-selected view among many. From your description, it sounds like you're manipulating this publicly exposed, manually addressable item to manipulate different parts of your viewport, while leaving others alone. That's not how it works.

    One of the nice things about Backbone is that if you pass it a route that's already a regular expression, it will use it as-is. So if you're trying to use the controller to create a bookmarkable description of the layout (component 1 in the upper right hand corner in display mode "A", component 2 in the upper left corner in display mode "B", etc) I can suggest a number of alternatives-- allocate each one a namespace in the hash part of the URL, and create routes that ignore the rest, i.e.

    routes: {
        new RegExp('^([^\/]*)/.*$'): 'doComponent1stuff',
        new RegExp('^[^\/]*/([^\/]*)\/.*$': 'doComponent2stuff',
    }
    

    See how the first uses only items after the first slash, the second after the second slash, etc. You can encode your magic entirely how you want.

    I suggest, though, that if you're going to be doing something with the look and feel of the components, and you want that to be reasonably persistent, that you look into the views getting and setting their cookies from some local store; if they're small enough, cookies will be enough.

    0 讨论(0)
  • 2021-01-31 07:02

    I've used namespacing to deal with a similar problem. Each module comes with it's own module controller, but is restricted to handle routes that start with /moduleName/ this way modules can be developed independently.

    0 讨论(0)
提交回复
热议问题