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

Routers: _bindRoutes() after initialize function call #3877

Closed
philolt opened this issue Dec 3, 2015 · 3 comments
Closed

Routers: _bindRoutes() after initialize function call #3877

philolt opened this issue Dec 3, 2015 · 3 comments

Comments

@philolt
Copy link

philolt commented Dec 3, 2015

Wouldn't it be better to call _bindRoutes() after the initialization.

var Router = Backbone.Router = function(options) {
    options || (options = {});
    if (options.routes) this.routes = options.routes;
    this._bindRoutes();
    this.initialize.apply(this, arguments);
  };

I try to use object-methods in the route object. The initialization of the objects is in the initialize method. The route-object looks like this:

route: {
    'path/to/:id': this.object.method
  {

This causes an Error because the Object doesn't exist at the time of the _bindRoutes() call. Currently I'm using a small Workaroud. I set route in the initialize method and then call _bindRoutes() manually.

@paulfalgout
Copy link
Collaborator

initialization should pretty much always be last. However you may be interested in #3827

In your case though you could define the routes in the initialize using http://backbonejs.org/#Router-route
You could also potentially pass the routes in when instantiating assuming you have the object in question at that point.

new Backbone.Router({ 
  routes: { 
    'path/to/:id': myObject.method
  },
  object: myObject 
});

@philolt
Copy link
Author

philolt commented Dec 12, 2015

Thanks for the response!
I thought in this special case it makes sense to call _bindRoutes after the initialization, because it is something that may depend on the initialize method. If it's a convention always to call it in the end of the constructor I have to do it like you said.

@paulfalgout
Copy link
Collaborator

@akre54 can be closed :-)

@akre54 akre54 closed this as completed Mar 18, 2017
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

3 participants