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

Allow controllers outside of the current namespace using absolute paths #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/locomotive/namespace.js
Expand Up @@ -52,9 +52,14 @@ Namespace.prototype.qpath = function(path) {
* @api protected * @api protected
*/ */
Namespace.prototype.qcontroller = function(controller) { Namespace.prototype.qcontroller = function(controller) {
var root = false;
if (controller[0] === '/') {
controller = controller.slice(1);
root = true;
}
var qual = utils.controllerize(controller); var qual = utils.controllerize(controller);
var ns = this; var ns = this;
while (ns) { while (ns && ! root) {
qual = (ns.module.length) ? (ns.module + '::' + qual) : (qual); qual = (ns.module.length) ? (ns.module + '::' + qual) : (qual);
ns = ns.parent; ns = ns.parent;
} }
Expand Down
20 changes: 16 additions & 4 deletions lib/locomotive/router.js
Expand Up @@ -208,7 +208,13 @@ Router.prototype.match = function(pattern, shorthand, options) {
* @param {String} name * @param {String} name
* @api public * @api public
*/ */
Router.prototype.resource = function(name, fn) { Router.prototype.resource = function(name, options, fn) {
if (typeof options === 'function') {
fn = options;
options = {};
}
options = options || {}

var ns = this._ns[this._ns.length - 1] var ns = this._ns[this._ns.length - 1]
, path = ns.qpath(name) , path = ns.qpath(name)
, controller = ns.qcontroller(name) , controller = ns.qcontroller(name)
Expand All @@ -225,7 +231,7 @@ Router.prototype.resource = function(name, fn) {
this._route('put' , path , controller, 'update' ); this._route('put' , path , controller, 'update' );
this._route('del' , path , controller, 'destroy'); this._route('del' , path , controller, 'destroy');


this.namespace(name, { module: null, helper: name }, function() { this.namespace(name, { module: options.namespace ? name : null, helper: name }, function() {
fn && fn.call(this); fn && fn.call(this);
}); });
} }
Expand Down Expand Up @@ -274,7 +280,13 @@ Router.prototype.resource = function(name, fn) {
* @param {String} name * @param {String} name
* @api public * @api public
*/ */
Router.prototype.resources = function(name, fn) { Router.prototype.resources = function(name, options, fn) {
if (typeof options === 'function') {
fn = options;
options = {};
}
options = options || {}

var ns = this._ns[this._ns.length - 1] var ns = this._ns[this._ns.length - 1]
, singular = lingo.en.singularize(name) , singular = lingo.en.singularize(name)
, path = ns.qpath(name) , path = ns.qpath(name)
Expand All @@ -297,7 +309,7 @@ Router.prototype.resources = function(name, fn) {
// TODO: I think placeholder should be underscored. Test for this. // TODO: I think placeholder should be underscored. Test for this.
// TODO: Implement support for dasherized names. // TODO: Implement support for dasherized names.
placeholder = ':' + utils.helperize(singular) + '_id'; placeholder = ':' + utils.helperize(singular) + '_id';
this.namespace(name + '/' + placeholder, { module: null, helper: singular }, function() { this.namespace(name + '/' + placeholder, { module: options.namespace ? name : null, helper: singular }, function() {
fn && fn.call(this); fn && fn.call(this);
}); });
} }
Expand Down