multiple matching routes

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

提交回复
热议问题