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

Enable possibility to pass an empty prefix #34

Merged
merged 1 commit into from
Mar 28, 2014

Conversation

maoueh
Copy link

@maoueh maoueh commented Mar 21, 2014

This feature could be used to create a main router that delegates unmatched paths to a "global" router. The main router would then only contains sub router definitions. I put an example usage at the bottom.

Tell me if it's something you would accept. Maybe there is already a better idea to achieve this. Sorry in advance for any missing things, first time I contribute to a JavaScript project :)

Example Usage:

var Router = Backbone.Router.extend({
    routes: {
      // Sub paths
      "projects/*subroute" : "routeProjects",

      // Fallback to MainRouter
      "*subroute" : "routeMain",
    },

    routeMain : function (subroute) {
        if (!App.Routers.Main) {
          App.Routers.Main = new MainRouter("");
        }
    },

    routeProjects : function (subroute) {
        if (!App.Routers.Projects) {
          App.Routers.Projects = new ProjectsRouter("projects");
        }
    }
  });

Regards,
Matt

 * This feature can be use to match sub route than delegate unmatched
   paths to a "global" router. The main router would then only contains
   sub router definitions.
 * I would use this in a project like this:

    // Main router definition
    routes: {
      "projects*subroute" : "routeProjects",

      "*subroute" : "routeHome",
    },

    // When needed
    new HomeRouter("");

   If route does not start with "projects", fallback to home
   router which.
@geekdave
Copy link
Contributor

@maoueh thanks for this!

geekdave added a commit that referenced this pull request Mar 28, 2014
Enable possibility to pass an empty prefix
@geekdave geekdave merged commit 29f448a into BackboneSubroute:master Mar 28, 2014
@maoueh
Copy link
Author

maoueh commented Mar 28, 2014

I'm glad to help out.

Another use case I found is that you can now use Backbone.SubRoute as your "main" router. For example, you can have:

MyApp.Router = Backbone.SubRoute.extend({
       ...
});

instead of:

MyApp.Router = Backbone.Router.extend({
       ...
});

And instantiating with new MyApp.Router(), the prefix is empty and with the patch, they are added correctly without the leading slash. No big deal but it's cool for consistency.

Regards,
Matt

@geekdave
Copy link
Contributor

Nice!

@maoueh maoueh deleted the feature-empty-prefix branch April 8, 2014 03:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants