Coder Social home page Coder Social logo

backbonesubroute / backbone.subroute Goto Github PK

View Code? Open in Web Editor NEW
244.0 244.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 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

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

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!

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.

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.

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??
    },
});

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?

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...

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.

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

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

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

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

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.

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 :)

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!

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 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.

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.

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'

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.

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.