Coder Social home page Coder Social logo

backbonesubroute / backbone.subroute Goto Github PK

View Code? Open in Web Editor NEW
244.0 18.0 60.0 188 KB

Backbone.subroute extends the functionality of Backbone.router such that each of an application's modules can define its own module-specific routes.

License: MIT License

JavaScript 91.44% HTML 8.56%

backbone.subroute's Introduction

#Backbone Subroute

No Maintenance Intended

Unmaintained!

This project is no longer being maintained. Feel free to fork and continue development.

##About

Backbone's Router feature is really powerful, but for large multi-module apps, it can quickly become large and unmaintainable.

Backbone.SubRoute extends the functionality of Backbone.Router such that each of an application's modules can define its own module-specific routes. This eliminates the need for one monolithic Router configuration, The base router can instead act as a simple delegator that forwards module-specific routes to the appropriate module-specific SubRoute.

For example, given this URL: http:example.org/myModule/foo/bar

...the base router would be responsible for invoking and delegating to the proper module based on "myModule". The module would then have its own SubRoute which would have its own mappings for the foo/bar part.

This project is based on a Gist by Tim Branyen, and used with permission from the original author.

Subroute is lightweight, weighing in at under 300 bytes minified and gzipped.

Downloads

Latest Stable Release: 0.4.5

Unreleased Edge Version (master)

Visit the project repo to download the latest unreleased code (may be unstable).

Get Involved!

Mailing List

Join the Backbone.Subroute Google Group to discuss new features and stay-up-to-date on project updates.

Ways to Contribute

Has Subroute been helpful to you? If you'd like to give back, here are a few ways:

  1. Blog about your experiences using Subroute, and let us know about it!
  2. Create a demo app using Subroute and add it to the repo.
  3. Improve the docs in the README.
  4. Fix a bug or add a new feature and submit a pull request (see below)

Pull Requests

Pull requests are welcome. For any significant change or new feature, please start a discussion in the Google Group, or log an issue first. This will save you some time, in case your idea is deemed not general enough to be included in the project.

Before submitting a pull request, please:

  1. Write unit tests to cover any new or modified lines of code, and add it to the specs/ directory. See the Testing section for more info.
  2. Run the test specs to make sure everything works. You can fire up a local web server, and point your browser to http://localhost:<port>/spec/
  3. Run the Grunt task to lint, test, and run code coverage on the project. See the Build section for more info.

Build

Grunt

Subroute uses Grunt to verify each build. If you are not familiar with Grunt, check out the getting started guide for an introduction and installation instructions.

Before submitting a pull request, please run the grunt task. To do so:

First, install local development dependencies. From the root Subroute directory, run:

npm install

then

grunt

Grunt will perform these tasks:

  • Beautify

To reduce merging and styling issues related to whitespace and formatting, the jsbeautifier task normalizes all formatting in project source. If you fail to run grunt prior to check-in, and any files have not been beautified, Travis-CI will reject the checkin.

  • Uglify

The code will be minified and saved to dist/backbone.subroute.min.js.

  • Lint

Javascript files are checked for errors using JSHint. The JSLint configuration is driven by the .jshintrc file.

  • Test

Test specs are run headlessly using PhantomJS

  • Coverage

Code coverage is enforced using Istanbul. Currently not every feature of Subroute has coverage.
But the enforced coverage levels are set such that code coverage cannot decrease. Over time, more tests will be added to increase coverage to as close to 100% as possible (help with this is greatly appreciated!)

Travis-CI

The Grunt build is run automatically using Travis-CI upon every pull request and push to master. But if any errors are found, you'll need to fix them and re-submit your pull request. So please run the grunt task locally to save time.

Defining a SubRouter

Let's say that you've got a section of your web app multiple pieces of functionality all live under the URL prefix http://yourserver.org/books. Here's how you'd create your subrouter.

var BooksRouter = Backbone.SubRoute.extend({
    routes: {
        ""               : "showBookstoreHomepage",
        "search"         : "searchBooks",
        "view/:bookId"   : "viewBookDetail",
    },
    showBookstoreHomepage: function() {
        // ...module-specific code
    },
    searchBooks: function() {
        // ...module-specific code
    },
    viewBookDetail: function(bookId) {
        // ...module-specific code using bookId param
    }
});

Note that the subrouter, itself, doesn't know or care what its prefix is. The prefix is only provided when instantiating specific instances of the sub-router. This makes sub-routers especially useful for reusing common pieces of code.

Creating an Instance

To use your sub-router, simply call its constructor and provide a prefix, like so:

var mySubRouteInstance = new BooksRouter("books");

Understanding The Magic

Given the example above, with an instance of the sub-route being created with the "books" argument passed as a constructor, and deployed on a server with a base URL of yourserver.org, this results in the following routes being created:

SubrouteExpands ToMatches URL
""bookshttp://yourserver.org/books
"search"books/searchhttp://yourserver.org/books/search
"view/:bookId"books/view/:bookIdhttp://yourserver.org/books/view/1234

Passing Parameters to SubRouter Instances

When instantiating a SubRouter, you may wish to provide some parameters from the calling code, which may be needed in your sub-router code. The sub-router constructor takes an optional object literal as a second argument.

For instance:

var mySubRouteInstance = new BooksRouter("books", {locale: "en_US", isVIP: true));

Then in your sub-router definition, you can access these parameters using the familiar Backbone options object, used in other Backbone components. Like so:

initialize: function(options) {
    this.locale = options.locale;
    this.isVIP = options.isVIP;
}

Thanks to @carpeliam for this feature.

Trailing Slashes

Backbone treats routes with trailing slashes as totally different routes than those without. For instance, these are two different routes in Backbone:

"search"

"search/"

URL semantics aside, many people have found this behavior annoying. If you're one of them, and you want to support either format without duplicating each and every one of your routes, you can pass the optional createTrailingSlashRoutes parameter when you instantiate your sub-router.

For example:

var mySubRouteInstance = new BooksRouter("books", {createTrailingSlashRoutes: true});

Using the examples above, a URL of either http://yourserver.org/books/search or http://yourserver.org/books/search/ would fire the searchBooks() callback.

Testing

The spec directory in the repo contains a suite of test specs. To run them, start a web server in the project directory, then point your browser to the spec directory.

The test specs can also be run online here.

Examples & Tutorials

Version History

0.4.6

Released 29 April 2015

  • Allows for using Backbone.history.loadUrl() into an uninitialized subRoute (Thanks, oskbor!)

0.4.5

Released 13 October 2014

0.4.4

Released 1 September 2014

  • Add official bower support
  • Add grunt-version to dev dependencies

0.4.3

Released 9 May 2014

  • Fix issue 38 due to minified dist file being out-of-sync from latest source.

0.4.2

Released 7 April 2014

0.4.1

Released 23 September 2013

  • Fix Issue #27, which caused multiple instances of the same subrouter class to get polluted with each others routes in their routes hash. Big thanks to @mikesnare for suggesting the fix, and @joshuaballoch for providing the specs to identify the issue.

0.4.0

Released 17 July 2013

  • Fix Issue #22, a bug that would cause initial loading to not happen correctly if prefix contained parameters. Thanks, @whyohwhyamihere for the PR!
  • Add build support using Grunt

0.3.2

Released 22 January 2013

  • Skip call to loadUrl on SubRoute init if history hash does not match SubRoute prefix

0.3.1

Released 26 October 2012

  • Fixed Issue #13. This was an IE8-only issue where manually including a trailing slash in a subroute prefix caused a double slash to appear in the fully-qualified route. Thanks to @mikesnare for logging the issue and providing the fix!
  • Added Jasmine test specs for above case

0.3

Released 30 August 2012

  • Fixed issue in navigate method where separator / character was omitted after prefix
  • Added Jasmine test specs

0.2

Released 15 July 2012

  • AMD support
  • Added createTrailingSlashRoutes property
  • No longer append trailing slash to route prefix (to support default empty-string routes)
  • Robust handling of subroute path creation, supporting prefixes with and without trailing slashes

0.1

Released 16 April 2012

  • Initial version based on Tim's gist, with a fix for invoking a subroute on first load

##License The MIT License (MIT)

Copyright (c) 2012 Model N, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

backbone.subroute's People

Contributors

aeksco avatar carpeliam avatar davbo avatar davidosomething avatar gdw2 avatar geekdave avatar jamiebuilds avatar joshuaballoch avatar jrf0110 avatar jtuulos avatar kalley avatar leptonix avatar mikesnare avatar neboleft avatar tomfuertes avatar xmartin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

backbone.subroute's Issues

Seeking New Ownership!

This project is no longer actively maintained. If you are interested in taking it over, please comment below. Highlight any contributions you have made to the project, as well as any other examples of your open source work. Thanks!

Backbone.history.start and this.options

Sooo, I'm confused about the order of operations here:

window.router = new MyApp.Routers.Main('/',{foo:'bar'});
Backbone.history.start();

This causes an error because the Main router is using Backbone.history.loadUrl( hash ); on line 76 of backbone.subroute.js, and that in turn looks for Backbone's this.options (in Backbone's getFragment method). this.options is undefined until you call Backbone.history.start();.

However, you can't call Backbone.history.start(); until you have set up a router--history is undefined then. An example app would really help! What's going on? It seems like Backbone.SubRoute has a catch-22 built into it.

Reverse Feature

I would like to add a reverse feature that let's you dynamically generate the url from "name" and some "arguments". It should be very similar to Django's reverse feature: https://docs.djangoproject.com/en/1.4/topics/http/urls/#django.core.urlresolvers.reverse.

Use Case for reverse:
A url for a specific route is variable for various reasons: it could be that during the implementation project, the url signature changes, e.g. from details/:id to item/:id. But also the prefix could change, if the application is hooked into a different master application. E.g if you are only delaing with books, you do not need a prefix; but if you are dealing with books, music and bikes, you definitely need a prefix. And depending on the complexity of the application this would require quite some rework in the rest of the code.
Usage:
The reverse signature is reverse(name, options). Where name should be the stable name to call a specific method or represent a specific state of the application; e.g. "summary". And options should represent any detailed information like e.g. {id:1}. So reverse("summary",{id:1}) could return books/summary_view/id_1.

Reverse should work in 2 ways:
a) By providing a reverse object to options that has a structure like {name:templatestring}; name would then be "summary" and template_string would be "summary_view/id<%=id&>" (books is only the prefix).
b) If there is no reverse options provided for the specific name, it will try to be reengineered from the routes; which is only possible if not provided as a regexp .

Please check my fork at https://github.com/schacki/backbone.subroute. It is not tested yet, but feature wise it should be complete

Route gets triggered twice

Hello,

I am using 0.4.5 and when I load my app from route/subroute the route gets triggered twice. Once when the subrouter gets initialised and one when I do Backbone.history.start().
Is this a known problem?

Specify createTrailingSlashRoutes as an instance property when extending from Backbone.Subroute

I'm in a situation where I have the following:

class App.Routers.SuperRouter extends Backbone.SubRoute
  createTrailingSlashRoutes: true
  ...

from which I extend:

class App.Routers.Books extends App.Routers.SuperRouter
  ...

I've noticed that the createTrailingSlashRoutes: true instance property is applied to neither App.Routers.SuperRouter nor the routers extending from it. This appears to be the result of backbone.subroute.js line 38:

this.createTrailingSlashRoutes = options && options.createTrailingSlashRoutes;

indicating that the createTrailingSlashRoutes option must be supplied to the router when it's instantiated. It looks like this would be a simple change:

this.createTrailingSlashRoutes ||= options && options.createTrailingSlashRoutes;

and I would like to know wether this is worth doing before I go through the process of throwing together a PR. For the time being I am supplying my own constructor:

class App.Routers.SuperRouter extends Backbone.SubRoute
  constructor: (route, opts = {}) ->
    super(route, _.extend opts, { createTrailingSlashRoutes: true })
  ...

Let me know your thoughts when you've got a moment - thanks!

Routes with named params

How can I use subroutes with named params

routes : {
  ':country/transaction' : 'showTransaction',
  ':country/reports' : 'showReports'
}

if I am using subroutes, how can I get the value of :country in my action?

History inside a subroute

Hi,

I was planning on having an individual history stack per sub route so that if I switch to another route in the root router it will remember which route that was the latest inside of that router. Is this possible in any way? Backbone.history.start() doesn't seem to be possible to call inside of a sub router without getting errors. That was my simple idea of how to do it. Any ideas about this?

Thanks
David

Initializing a subrouter within a route handler results in an infinite loop/stack overflow in IE

The advertised pattern for using SubRoute so far has been

MyApp.Router = Backbone.Router.extend({
  routes: {
    "books/*subroute"  : "invokeBooksModule"
  },
  invokeBooksModule: function(subroute) {
    if (!MyApp.Routers.Books) { 
      MyApp.Routers.Books = new MyApp.Books.Router("books/");
    }
  }
});

I've run into a stack overflow in IE using this approach, when directly loading a page for a subroute. While debugging in IE is a little difficult, here's my impression of what I think is happening:

  1. MyApp.Router receives a call to #books/*subroute (#books/search for example)
  2. MyApp.Router routes this to invokeBooksModule
  3. MyApp.Router initializes a new Books router
  4. The books router calls Backbone.history.loadUrl
  5. Backbone.history.loadUrl looks through the routes for a match
  6. It matches against "books/*subroute" in MyApp.Router
  7. MyApp.Router calls invokeBooksModule again
  8. Because the Books router's constructor hasn't returned, MyApp.Routers.Books is undefined, so it initializes a new Books router again

I haven't really gotten to the bottom of why things happen this way in IE but not in Chrome etc... unfortunately I don't have much time to debug this further myself, I might be able to look more later. I tried initializing my subroute in the initialize method of MyApp.Router, which I think might solve the recursion issue, but I ran into a separate exception that I raised in a separate issue.

loadUrl into uninitialized subroute fails

Hi!
In my main router I have the following route handlers:

routes: {
    'i(:source)/*subroute': 'setSource',
    'view*subroute': 'invokeView',
    'settings*subroute': 'invokeSettings'
  },
    invokeView : function (subroute) {
      if(!subRoutes.view){
        subRoutes.view = new ViewRouter ('view', 
          {createTrailingSlashRoutes: true});
      }
    },
    invokeSettings : function (subroute) {
      if(!subRoutes.settings){
        subRoutes.settings = new SettingsRouter ('settings', 
          {createTrailingSlashRoutes: true});
      }
    },
    setSource: function (source, subroute) {
      if(this.setSourceNumberIfValid(source)){
        console.log('set source to:', source, '.Routing through to:', subroute);
        Backbone.history.loadUrl(subroute); // <-- This doesn't work if the subRoute isn't initialized
      }
      else {
        console.log('invalid source: ', source);
      }
    }

I have currently hacked your great lib to support my use case. I did that by doing this change:

            // grab the full URL
            var hash;
            if (Backbone.history.fragment) {
                hash = Backbone.history.getFragment(Backbone.history.fragment); //Was called without an argument before
            } else {
                hash = Backbone.history.getHash();
            }

Not sure if this is a reasonable way to do it. Will it break someone else? Would you consider supporting this usecase?

br Oskar

Subroute fails if called before Backbone.history has been started

I tried initializing a subroute within the initialize of my main router, and this works fine if I start at the root and browse to a route covered by my subroute, but doesn't work if I load a subroute-related path initially. (So, if my subroute covers '#books' and I start at www.books.com and navigate to www.books.com/#books everything is fine; but if I load up www.books.com/#books in my browser, it throws an exception.) The reason for this is that Backbone.history.loadUrl references options.root which won't exist until after Backbone.history.start has been called. And I can't start Backbone.history before I've created my router either. This is with Backbone v0.9.2.

Your initial approach was to create the subroute in a handler of the main router, but I'm getting a stack overflow with that approach in IE (at least IE8, I think IE9 as well). I'll create a separate issue for that.

Regex routes

Regular expression routes are not being accepted. Only assumes the route is a string.

Polluted Routes when using a common base router class

Hi @geekdave

I have an application where I was testing this plugin. It's a great idea! I ran into a major issue, but it may just be relevant to my usecase.

If I define a Router:

class Router extends Backbone.SubRoute
  routes:
    "asdf":"test"

  test: () ->
    console.log 'test result positive'
    console.log @routes

And then I use it twice, under two prefixes:

   SubAppOne.router = new Router("subappone")
   SubAppTwo.router = new Router("subapptwo")

The second time I initialize a new Router, the routes are polluted with the routes from the first. EG:

SubAppOne.router.routes Yields {subappone/asdf:"test"} as it should, but SubAppTwo.router.routes Yields {subappone/asdf:"test", subapptwo/asdf:"test"}

As I said, maybe this is specific to my use case, but I am wondering if there is a feasible solution to this issue.

npm support

Hello,

Please, add npm support and push it into npm registry for a better use with browserify and webpack.

Thanks

Forget it, i misspelled on npm... thanks a lot.

handling zombie views in the subroute

Hi,

Thanks for sub route . Can you suggest me how do we handle the zombie views in
the sub route module ?

When I tried to close the existing created view, the current view also got destroyed because of common $el and it's not executing the render method .

Have you tried to destroy existing views ?

Raja K

Problem with Backbone AMD

Hi there,

I'm trying to implement an app with backbone subroute, but I'm getting an error that seems related to AMD.

After calling the subroutes on the main route file, I've got this error:

TypeError: this._bindRoutes is not a function
[Break On This Error]

...{});if(i.isString(this.comparator)||this.comparator.length===1){this.models=this...

Any ideas?

Fernão

Figure out getHash vs getFragment solution

We use this code to trigger a new subroute immediate upon initialization:

        // grab the full URL
        var hash = Backbone.history.getHash();

        // Trigger the subroute immediately.  this supports the case where 
        // a user directly navigates to a URL with a subroute on the first page load.
        Backbone.history.loadUrl( hash );

For some, only getHash() works. For others, only getFragment() works.

I haven't had the time to track down why, since some of these cases involve using push state, which I am not using in any projects. If anyone can help out with getting to the bottom of this, it would be a big help.

Some discussion on the issue, and a previous pull request, can be found here.

Thanks to @jtuulos and @MMRandy for your feedback so far.

Multiple SubRoutes - is it possible?

Is it possible to have multi-level subroutes with this module?
For example

main router has
"reports/*subroute"
and 2 or more modules


1. "operational/*subroute"
2. "monthly/*subroute"

then , each of submodules defines its own set of routes.
That would make
/reports/operational/a-report
/reports/operational/b-report
where a-report and b-report are defined as final routes...

Or can only 1 level submodule be defined?
If multiple levels are possible, is there an example...

Regards
Z

accessing router initialize parameters from the route callback?

I saw you can pass parameters to your subroute like so

var mySubRouteInstance = new BooksRouter("books", {locale: "en_US", isVIP: true));

Then retrieve it from the SubRoute like so

initialize: function(options) {
    this.locale = options.locale;
    this.isVIP = options.isVIP;
}

Now given my subroute, how can I access locale and isVIP from my subroute callback mycallback()?

var BooksRouter = Backbone.SubRoute.extend({
    initialize: function(options) {
         this.locale = options.locale;
    }
    routes: {
        "myroute"   : "mycallback",
    },
    mycallback: function() {
         //###### how can I get the value of locale here??
    },
});

using back button does not re-trigger all my routes

Situation: Loading the page from the URL triggers all my routes and subroutes but accessing that URL after pressing the back or forward button only triggers the last subroute.

Problem
Let's say I access http://host/path1/path2/path3 from within he App I click on a link which brings me to http://host/otherpath. When I press the back button it brings me back to http://host/path1/path2/path3 in the URL but only my ModuleThree.Router gets hit. I want all of them to get hit again, how can I do this?

  1. user loads url http://host/path1/path2/path3
inside one
inside two
inside three
  1. user clicks on link that goes to http://otherpath
  2. user presses back button
inside three

My problem is on step 3, I'm expecting all the subroutes to re-trigger for my app to function correctly but only the last sub-route is triggered.

Example: Below I setup my subrouters so when accessing
http://host/path1/path2/path3 I have 3 matches triggered total

/path1/*subroute handled by ModuleOne.Router (initialized by appInitiliazer)
/path2/*subroute handled by ModuleTwo.Router (initialized by ModuleOne.Router)
/path3/*subroute handled by ModuleThree.Router (initialized by ModuleTwo.Router)

  ## ROUTER
  class ModuleOne.Router extends App.Router
    routes:
         "path1/*subroute" : "pathOne"
    pathOne: (hash) ->
            console.log 'inside one'

      ## this gets triggered on URL enter if /path1 is in URL
      new App.ModuleTwo.Router("path1")

    new ModuleOne.Router

## -------------------------------------------------------

  ## ROUTER
  class ModuleTwo.Router extends App.SubRouter
    routes:
      "path2/*subroute" : "pathTwo"
    pathTwo: ->
      console.log 'inside two'
      new App.ModuleThree.Router("path1/path2")
## -------------------------------------------------------
  ## ROUTER
  class ModuleThree.Router extends App.SubRouter
    routes:
      "path3/*subroute" : "pathThree"  
    pathThree: ->
      console.log 'inside three'

Acknowledging support for lodash

I noticed this project is setup as an AMD with a dependency of Underscore. On the most recent (at least the most recent) version of Backbone, Lodash.js is supported as a replacement for Underscore.js. I have been able to replace this by simply changing the define as such:

(function (factory) {
    if (typeof define === 'function' && define.amd) {
        // Register as an AMD module if available...
        define(['lodash', 'backbone'], factory);
    } else {
        // Browser globals for the unenlightened...
        factory(_, Backbone);
    }
}...

My understanding is that Lodash is a drop-in replacement for underscore in the ways in which Backbone uses it.

Route empty does not match [solved]

I have the next code

AdmisionSubRouter = Backbone.SubRoute.extend({

routes: {
    ""                              : "main",
    "search"                             : "search"
},

main: function() {
    console.log("main");
},

search: function() {
    console.log("search");
}

When I write the url admision/ (admision calls to AdmisionSubRouter) the main function is not called, I dont see the log. If I write admision/search the search function is called.

I had to change in Backbone.SubRoute the function Backbone.history.getHash() to Backbone.history.fragment because the console told me that getHash was not defined, and the subrouter was not initialized.

Where is the problem?

Thanks

Problems when creating subrouter using prefix with trailing slash in IE8

Minor issue. The code to set the separator to either a slash ('/') or the empty string on lines 29-32 uses prefix.substr(-1). In IE8, substr returns the entire string when called with a negative index.

The result is that if you provide a prefix with a trailing slash all the nested routes will end up with double slashes in the matching regex.

The fix is simply to change from substr to slice. Let me know if you need a pull request for this. Since it's such a small fix I figured it might not be necessary.

Thanks...

Minified version produces wrong route

Hi

Affected Version: Bower 0.4.2

Case: Add a subrouter (prefix "", routes "" and "foo")

Expectation: Routes / and /foo are catched

Result: Using the minified version the regex in Backbone.history.handlers is /^/foo(?:?([\s\S]*))?$/ therefore /foo is NOT catched. Using the non minified version everything works like a charm.

Can you fix this?

Regards
Sven

How can I restart subroutes?

I mean, allways than a subroute is executed the first time, I evaluate If the subroute is allowed. If the subroute is allowed I create the subroute. If is not allowed the view does not change.
I would like to reset the initialized subroutes. I want to evaluate the permissions again after do something like appRouter.reset();

  MainRouter = Backbone.Router.extend({
      subRouters: {},

      routes: {
          "": "main",
          ":appName/*navPath": "loadApp"
      },

      loadApp: function (appName, navPath) {
          this.subRouters = this.subRouters || {};

          if (!this.subRouters[appName] && this.hasPermissions(appName))
               this.subRouters[appName] = new this.modules[appName](appName, {
                  createTrailingSlashRoutes: true,
                  appName: appName
              });
      },

I tried to do appRouter.subRouters = {}; but It doesn't work. The urls of subrouters already visited don't execute loadApp. I tried to restart the history, and I didn't get any result.

Please could you help me?

SubRoute causes routes to get evaluated multiple times

So I have this MainRouter.js:

var Router = Backbone.Router.extend({
            routes: {
                '': "root",
                users: "users",
                test: 'test',
                '*actions': 'defaultRoute'
            },

            test: function () {
                debugger;
            },

             createSubRouters: function () {
                require(['routers/CompanyRouter'], function (Router) {
                    new Router('companies', {createTrailingSlashRoutes: true});
                });
                 ...
             }

The router is loaded in an Marionette App, like this:
app.js:

           app.addInitializer(function () {
                // we require the router here to avoid circular dependencies
                require(['routers/MainRouter'], function (Router) {
                    app.router = new Router();
                    Backbone.history.start();
                    app.router.createSubRouters();
                })
            });

When I go on #/test, the debugger is triggered when starting the history in app.js.
However, when the subrouters are created, Backbone.history.loadUrl (https://github.com/ModelN/backbone.subroute/blob/master/backbone.subroute.js#L81) causes the function to be executed again.

This means, that when I first time load my app, the controller/router function is executed (1 + numberOfSubRouters) times.

Can I avoid this somehow? I tried to move the app.router.createSubRouters(); before the Backbone.history.start(), but with no success.

Any help is greatly appreciated :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.