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

accessing router initialize parameters from the route callback? #49

Open
newtonianb opened this issue Mar 6, 2015 · 3 comments
Open

Comments

@newtonianb
Copy link

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??
    },
});
@aeksco
Copy link
Member

aeksco commented Mar 6, 2015

You should be able access it with:

this.locale

Here's a functional example in CoffeeScript - different syntax, same principle:

App.Routers.Shop = Backbone.SubRoute.extend

  initialize: (options) ->
    @locale = options.locale

  routes:
    'books(/*subroute)'   : 'books'

  books: ->
    new App.Routers.Shop.Books("#{ @prefix }/books", { locale: @locale, createTrailingSlashRoutes: true })

@newtonianb
Copy link
Author

Hi @aeksco, sorry I got carried away and forgot critical details that I'm both using marionette and Marionette.Subrouter https://github.com/pushchris/backbone.marionette.subrouter ... which both are supposed to be extensions but anyways, I understand why this works in your example and also see now why it doesn't work in mine (below) but I still would love to find a solution.

Backbone.Router > Marionne.AppRouter > Marionette.SubRouter

Marionette Router derived from Backbone Router and Marionette SubRouter derives from AppRouter but clearly there's a ton of more things happening.

In my case using appRoutes this from inside my callback just gives me back the actual controller passed to the router or in this case below API which is an object with keys representing the functions

API =
  myPath: ->
   # debugger  # how can I access options here
class MyApp.Router extends Marionette.SubRouter
 controller: API
 initialize: (options) ->
   { @myvar } = options
 appRoutes:
   "my-path" : "myPath"

I guess this is a Marionette question

@aeksco
Copy link
Member

aeksco commented Mar 7, 2015

Interesting - wasn't familiar with Marionette.Subrouter. Learning new things everyday :D

Superficially the issue you're experiencing appears to be a result of your method not being properly bound to the subrouter instance.

For example, you have:

  myCallback: function() {
    console.log(this.foo);
  }

Where you really need:

  myCallback: (function(_this) {
    return function() {
      console.log(_this.foo);
    };
  })(this)

If I'm not mistaken, the above is typically going to be used when the method you're defining is a callback that references instance fields in the parent object.

The distinction in CoffeeScript is terse and fails to clarify the underlying principles, though researching the nuances between thin arrows and fat arrows will shed some light on your dilemma, given of course that the rest of the code is functioning well.

    myCallback: ->
        console.log @foo

VS

    myBoundCallback: =>
      console.log @foo

Hope this helps - good luck!

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

No branches or pull requests

2 participants