From 6d539c0afd4edf291c3da2d737efab04c788b459 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 6 Jan 2015 16:59:33 -0500 Subject: [PATCH] You don't need to call `buildRoutes` anymore. Just export a function in a module called `route-map` and discourse's rotuer will do the rest. This makes it a lot easier to define routes in plugins. --- ...admin_routes.js => admin-route-map.js.es6} | 4 +-- ...ication_routes.js => app-route-map.js.es6} | 34 +++++++------------ .../discourse/routes/discourse_route.js | 18 +++++++++- 3 files changed, 32 insertions(+), 24 deletions(-) rename app/assets/javascripts/admin/routes/{admin_routes.js => admin-route-map.js.es6} (97%) rename app/assets/javascripts/discourse/routes/{application_routes.js => app-route-map.js.es6} (74%) diff --git a/app/assets/javascripts/admin/routes/admin_routes.js b/app/assets/javascripts/admin/routes/admin-route-map.js.es6 similarity index 97% rename from app/assets/javascripts/admin/routes/admin_routes.js rename to app/assets/javascripts/admin/routes/admin-route-map.js.es6 index d981836564a..8d868d7115b 100644 --- a/app/assets/javascripts/admin/routes/admin_routes.js +++ b/app/assets/javascripts/admin/routes/admin-route-map.js.es6 @@ -1,4 +1,4 @@ -Discourse.Route.buildRoutes(function() { +export default function() { this.resource('admin', function() { this.route('dashboard', { path: '/' }); this.resource('adminSiteSettings', { path: '/site_settings' }, function() { @@ -60,4 +60,4 @@ Discourse.Route.buildRoutes(function() { }); }); -}); +} diff --git a/app/assets/javascripts/discourse/routes/application_routes.js b/app/assets/javascripts/discourse/routes/app-route-map.js.es6 similarity index 74% rename from app/assets/javascripts/discourse/routes/application_routes.js rename to app/assets/javascripts/discourse/routes/app-route-map.js.es6 index a7d0200d630..f73f132b14d 100644 --- a/app/assets/javascripts/discourse/routes/application_routes.js +++ b/app/assets/javascripts/discourse/routes/app-route-map.js.es6 @@ -1,12 +1,4 @@ -/** - Builds the routes for the application - - @method buildRoutes - @for Discourse.ApplicationRoute -**/ -Discourse.Route.buildRoutes(function() { - var router = this; - +export default function() { // Error page this.route('exception', { path: '/exception' }); @@ -20,7 +12,6 @@ Discourse.Route.buildRoutes(function() { this.resource('topicBySlug', { path: '/t/:slug' }); this.resource('discovery', { path: '/' }, function() { - router = this; // top this.route('top'); this.route('topCategory', { path: '/c/:slug/l/top' }); @@ -28,20 +19,21 @@ Discourse.Route.buildRoutes(function() { this.route('topCategory', { path: '/c/:parentSlug/:slug/l/top' }); // top by periods + var self = this; Discourse.Site.currentProp('periods').forEach(function(period) { var top = 'top' + period.capitalize(); - router.route(top, { path: '/top/' + period }); - router.route(top + 'Category', { path: '/c/:slug/l/top/' + period }); - router.route(top + 'CategoryNone', { path: '/c/:slug/none/l/top/' + period }); - router.route(top + 'Category', { path: '/c/:parentSlug/:slug/l/top/' + period }); + self.route(top, { path: '/top/' + period }); + self.route(top + 'Category', { path: '/c/:slug/l/top/' + period }); + self.route(top + 'CategoryNone', { path: '/c/:slug/none/l/top/' + period }); + self.route(top + 'Category', { path: '/c/:parentSlug/:slug/l/top/' + period }); }); // filters Discourse.Site.currentProp('filters').forEach(function(filter) { - router.route(filter, { path: '/' + filter }); - router.route(filter + 'Category', { path: '/c/:slug/l/' + filter }); - router.route(filter + 'CategoryNone', { path: '/c/:slug/none/l/' + filter }); - router.route(filter + 'Category', { path: '/c/:parentSlug/:slug/l/' + filter }); + self.route(filter, { path: '/' + filter }); + self.route(filter + 'Category', { path: '/c/:slug/l/' + filter }); + self.route(filter + 'CategoryNone', { path: '/c/:slug/none/l/' + filter }); + self.route(filter + 'Category', { path: '/c/:parentSlug/:slug/l/' + filter }); }); this.route('categories'); @@ -62,9 +54,9 @@ Discourse.Route.buildRoutes(function() { // User routes this.resource('user', { path: '/users/:username' }, function() { this.resource('userActivity', { path: '/activity' }, function() { - router = this; + var self = this; _.map(Discourse.UserAction.TYPES, function (id, userAction) { - router.route(userAction, { path: userAction.replace('_', '-') }); + self.route(userAction, { path: userAction.replace('_', '-') }); }); }); @@ -99,4 +91,4 @@ Discourse.Route.buildRoutes(function() { this.resource('badges', function() { this.route('show', {path: '/:id/:slug'}); }); -}); +} diff --git a/app/assets/javascripts/discourse/routes/discourse_route.js b/app/assets/javascripts/discourse/routes/discourse_route.js index 96d0e302eac..c3af37a09c9 100644 --- a/app/assets/javascripts/discourse/routes/discourse_route.js +++ b/app/assets/javascripts/discourse/routes/discourse_route.js @@ -90,7 +90,23 @@ Discourse.Route.reopenClass({ mapRoutes: function() { Discourse.Router.map(function() { - routeBuilder.call(this); + var router = this; + + if (routeBuilder) { + Ember.warn("The Discourse `routeBuilder` is deprecated. Export a `route-map` instead"); + routeBuilder.call(router); + } + + // If a module is defined as `route-map` in discourse or a plugin, its routes + // will be built automatically. + Ember.keys(requirejs._eak_seen).forEach(function(key) { + if (/route-map$/.test(key)) { + var module = require(key, null, null, true); + if (!module) { throw new Error(key + ' must export a map function.'); } + module.default.call(router); + } + }); + this.route('unknown', {path: '*path'}); }); },