From edfbfd733681424067dbb074d8984555ba5ccb44 Mon Sep 17 00:00:00 2001 From: Jacob Gable Date: Wed, 14 May 2014 20:56:43 -0500 Subject: [PATCH] Add ghostPaths util and initializer No Issue - Move the ghostPaths from base model to utils - Add initializer that injects it into every route, model and controller - Add a adminUrl and apiUrl helper method --- ghost/admin/app.js | 2 ++ ghost/admin/initializers/ghost-paths.js | 13 +++++++++++ ghost/admin/models/base.js | 12 +--------- ghost/admin/utils/ghost-paths.js | 29 +++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 ghost/admin/initializers/ghost-paths.js create mode 100644 ghost/admin/utils/ghost-paths.js diff --git a/ghost/admin/app.js b/ghost/admin/app.js index c2aac127c6..a7ed760515 100755 --- a/ghost/admin/app.js +++ b/ghost/admin/app.js @@ -4,6 +4,7 @@ import injectCurrentUser from 'ghost/initializers/current-user'; import injectCsrf from 'ghost/initializers/csrf'; import {registerNotifications, injectNotifications} from 'ghost/initializers/notifications'; import registerTrailingLocationHistory from 'ghost/initializers/trailing-history'; +import injectGhostPaths from 'ghost/initializers/ghost-paths'; import 'ghost/utils/link-view'; import 'ghost/utils/text-field'; @@ -24,6 +25,7 @@ initFixtures(); App.initializer(injectCurrentUser); App.initializer(injectCsrf); +App.initializer(injectGhostPaths); App.initializer(registerNotifications); App.initializer(injectNotifications); App.initializer(registerTrailingLocationHistory); diff --git a/ghost/admin/initializers/ghost-paths.js b/ghost/admin/initializers/ghost-paths.js new file mode 100644 index 0000000000..0c1703b692 --- /dev/null +++ b/ghost/admin/initializers/ghost-paths.js @@ -0,0 +1,13 @@ +import ghostPaths from 'ghost/utils/ghost-paths'; + +export default { + name: 'ghost-paths', + + initialize: function (container) { + container.register('ghost:paths', ghostPaths(), {instantiate: false}); + + container.injection('route', 'ghostPaths', 'ghost:paths'); + container.injection('model', 'ghostPaths', 'ghost:paths'); + container.injection('controller', 'ghostPaths', 'ghost:paths'); + } +}; \ No newline at end of file diff --git a/ghost/admin/models/base.js b/ghost/admin/models/base.js index 9a70b16b27..64c594ebc4 100644 --- a/ghost/admin/models/base.js +++ b/ghost/admin/models/base.js @@ -1,14 +1,4 @@ - -function ghostPaths() { - var path = window.location.pathname, - subdir = path.substr(0, path.search('/ghost/')); - - return { - subdir: subdir, - adminRoot: subdir + '/ghost', - apiRoot: subdir + '/ghost/api/v0.1' - }; -} +import ghostPaths from 'ghost/utils/ghost-paths'; var BaseModel = Ember.Object.extend({ diff --git a/ghost/admin/utils/ghost-paths.js b/ghost/admin/utils/ghost-paths.js new file mode 100644 index 0000000000..1a6188b74a --- /dev/null +++ b/ghost/admin/utils/ghost-paths.js @@ -0,0 +1,29 @@ +var makeRoute = function (root, args) { + var parts = Array.prototype.slice.call(args, 0).join('/'), + route = [root, parts].join('/'); + + if (route.slice(-1) !== '/') { + route += '/'; + } + + return route; +}; + +export default function ghostPaths() { + var path = window.location.pathname, + subdir = path.substr(0, path.search('/ghost/')); + + return { + subdir: subdir, + adminRoot: subdir + '/ghost', + apiRoot: subdir + '/ghost/api/v0.1', + + adminUrl: function () { + return makeRoute(this.adminRoot, arguments); + }, + + apiUrl: function () { + return makeRoute(this.apiRoot, arguments); + } + }; +} \ No newline at end of file