{"action":"created","issue":{"url":"https://api.github.com/repos/jashkenas/backbone/issues/3601","labels_url":"https://api.github.com/repos/jashkenas/backbone/issues/3601/labels{/name}","comments_url":"https://api.github.com/repos/jashkenas/backbone/issues/3601/comments","events_url":"https://api.github.com/repos/jashkenas/backbone/issues/3601/events","html_url":"https://github.com/jashkenas/backbone/issues/3601","id":75804057,"number":3601,"title":"Advice: add a function in Router to give users a chance to populate callbacks for routes","user":{"login":"freegink","id":3389244,"avatar_url":"https://avatars.githubusercontent.com/u/3389244?v=3","gravatar_id":"","url":"https://api.github.com/users/freegink","html_url":"https://github.com/freegink","followers_url":"https://api.github.com/users/freegink/followers","following_url":"https://api.github.com/users/freegink/following{/other_user}","gists_url":"https://api.github.com/users/freegink/gists{/gist_id}","starred_url":"https://api.github.com/users/freegink/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/freegink/subscriptions","organizations_url":"https://api.github.com/users/freegink/orgs","repos_url":"https://api.github.com/users/freegink/repos","events_url":"https://api.github.com/users/freegink/events{/privacy}","received_events_url":"https://api.github.com/users/freegink/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-05-13T01:30:09Z","updated_at":"2015-05-13T14:56:51Z","closed_at":"2015-05-13T09:51:22Z","body":"## Suggested change in Router\r\nAdd a new function 'callback' or whatever name you like in Router that users can override to populate callbacks according to the name that users specified in \"routes\". This change won't affect any existing projects that are using Backbone.\r\n```\r\n route: function(route, name, callback) {\r\n      if (!_.isRegExp(route)) route = this._routeToRegExp(route);\r\n      if (_.isFunction(name)) {\r\n        callback = name;\r\n        name = '';\r\n      }\r\n      if (!callback) callback = this.callback(name);   /* changed from \"this[name]\" */\r\n     ...\r\n      return this;\r\n    },\r\n\r\n/* \r\nUsers can override this function to populate callback functions according to the name. \r\n*/\r\ncallback: function(name) {\r\n    return this[name];\r\n}\r\n```\r\n\r\n## A simple example\r\n\r\n### Modules\r\n```\r\nvar modules = {\r\n    Index : {\r\n        home: function() {\r\n        }\r\n    },\r\n    Account: {\r\n        login: function() {\r\n        }\r\n    }\r\n};\r\n```\r\n\r\n### Router module\r\n```\r\nvar Router = Backbone.Router.extend({\r\n    routes: {\r\n        '': 'Index.home',\r\n        'login': 'Account.login',\r\n    },\r\n    callback: function(name) {\r\n        return function(args) {\r\n            var names = name.split('.');      \r\n            modules[names[0]][names[1]](args);\r\n        };\r\n    }\r\n});\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/jashkenas/backbone/issues/comments/101702406","html_url":"https://github.com/jashkenas/backbone/issues/3601#issuecomment-101702406","issue_url":"https://api.github.com/repos/jashkenas/backbone/issues/3601","id":101702406,"user":{"login":"akre54","id":931368,"avatar_url":"https://avatars.githubusercontent.com/u/931368?v=3","gravatar_id":"","url":"https://api.github.com/users/akre54","html_url":"https://github.com/akre54","followers_url":"https://api.github.com/users/akre54/followers","following_url":"https://api.github.com/users/akre54/following{/other_user}","gists_url":"https://api.github.com/users/akre54/gists{/gist_id}","starred_url":"https://api.github.com/users/akre54/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akre54/subscriptions","organizations_url":"https://api.github.com/users/akre54/orgs","repos_url":"https://api.github.com/users/akre54/repos","events_url":"https://api.github.com/users/akre54/events{/privacy}","received_events_url":"https://api.github.com/users/akre54/received_events","type":"User","site_admin":false},"created_at":"2015-05-13T14:56:51Z","updated_at":"2015-05-13T14:56:51Z","body":"The `name` change is only on master, it hasn't yet been added to a release. Our documentation on the website only reflects the current released version.\r\n\r\nWe [should really](https://github.com/jashkenas/backbone/pull/3285) release a version bump soon...."}}