{"version":3,"file":"marionette.approuter.js","sources":["src/marionette.approuter.js"],"sourcesContent":["// App Router\n// ----------\n\n// Reduce the boilerplate code of handling route events\n// and then calling a single method on another object,\n// called a controller.\n// Have your routers configured to call the method on\n// your controller, directly.\n//\n// Configure an AppRouter with `appRoutes`.\n//\n// App routers can only take one `controller` object.\n// It is recommended that you divide your controller\n// objects in to smaller pieces of related functionality\n// and have multiple routers / controllers, instead of\n// just one giant router and controller.\n//\n// You can also add standard routes to an AppRouter.\n\nimport Backbone from 'backbone';\nimport _ from 'underscore';\nimport Marionette, { MnObject } from 'backbone.marionette';\n\n// API borrowed from Marionette.Object\nconst ObjectAPI = [\n 'triggerMethod',\n 'normalizeMethods',\n '_setOptions',\n 'mergeOptions',\n 'getOption',\n 'bindEvents',\n 'unbindEvents'\n];\n\nconst ClassOptions = [\n 'appRoutes',\n 'controller'\n];\n\nconst AppRouter = Backbone.Router.extend({\n\n constructor(options) {\n this._setOptions(options);\n\n this.mergeOptions(options, ClassOptions);\n\n Backbone.Router.apply(this, arguments);\n\n const appRoutes = this.appRoutes;\n const controller = this._getController();\n this.processAppRoutes(controller, appRoutes);\n this.on('route', this._processOnRoute, this);\n },\n\n // Similar to route method on a Backbone Router but\n // method is called on the controller\n appRoute(route, methodName) {\n const controller = this._getController();\n this._addAppRoute(controller, route, methodName);\n return this;\n },\n\n // process the route event and trigger the onRoute\n // method call, if it exists\n _processOnRoute(routeName, routeArgs) {\n // make sure an onRoute before trying to call it\n if (_.isFunction(this.onRoute)) {\n // find the path that matches the current route\n const routePath = _.invert(this.appRoutes)[routeName];\n this.onRoute(routeName, routePath, routeArgs);\n }\n },\n\n // Internal method to process the `appRoutes` for the\n // router, and turn them in to routes that trigger the\n // specified method on the specified `controller`.\n processAppRoutes(controller, appRoutes) {\n if (!appRoutes) { return this; }\n\n const routeNames = _.keys(appRoutes).reverse(); // Backbone requires reverted order of routes\n\n _.each(routeNames, route => {\n this._addAppRoute(controller, route, appRoutes[route]);\n });\n\n return this;\n },\n\n _getController() {\n return this.controller;\n },\n\n _addAppRoute(controller, route, methodName) {\n const method = controller[methodName];\n\n if (!method) {\n throw new Error(`Method \"${methodName}\" was not found on the controller`);\n }\n\n this.route(route, methodName, _.bind(method, controller));\n }\n});\n\n_.extend(AppRouter.prototype, _.pick(MnObject.prototype, ObjectAPI));\n\n// For Backwards compatibility\nMarionette.AppRouter = AppRouter;\n\nexport default AppRouter;\n"],"names":["ObjectAPI","ClassOptions","AppRouter","Backbone","Router","extend","options","_setOptions","mergeOptions","apply","arguments","appRoutes","controller","_getController","processAppRoutes","on","_processOnRoute","route","methodName","_addAppRoute","routeName","routeArgs","_","isFunction","onRoute","routePath","invert","routeNames","keys","reverse","each","method","Error","bind","prototype","pick","MnObject","Marionette"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;;;;;;AAmBA,AAIA;AACA,IAAMA,YAAY,CAChB,eADgB,EAEhB,kBAFgB,EAGhB,aAHgB,EAIhB,cAJgB,EAKhB,WALgB,EAMhB,YANgB,EAOhB,cAPgB,CAAlB;;AAUA,IAAMC,eAAe,CACnB,WADmB,EAEnB,YAFmB,CAArB;;AAKA,IAAMC,YAAYC,SAASC,MAAT,CAAgBC,MAAhB,CAAuB;aAAA,uBAE3BC,OAF2B,EAElB;SACdC,WAAL,CAAiBD,OAAjB;;SAEKE,YAAL,CAAkBF,OAAlB,EAA2BL,YAA3B;;aAESG,MAAT,CAAgBK,KAAhB,CAAsB,IAAtB,EAA4BC,SAA5B;;QAEMC,YAAY,KAAKA,SAAvB;QACMC,aAAa,KAAKC,cAAL,EAAnB;SACKC,gBAAL,CAAsBF,UAAtB,EAAkCD,SAAlC;SACKI,EAAL,CAAQ,OAAR,EAAiB,KAAKC,eAAtB,EAAuC,IAAvC;GAZqC;;;;;UAAA,oBAiB9BC,KAjB8B,EAiBvBC,UAjBuB,EAiBX;QACpBN,aAAa,KAAKC,cAAL,EAAnB;SACKM,YAAL,CAAkBP,UAAlB,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