From a3973e9ff01cb019ce27c7c1947deea26a52bc97 Mon Sep 17 00:00:00 2001 From: lichengyin Date: Wed, 18 Nov 2015 13:00:45 +0800 Subject: [PATCH] change module/controller/action when invoke another action --- src/core/app.js | 8 ++++---- src/core/base.js | 22 +++++++++++++++++++--- src/core/http_base.js | 28 ++++++++++++++++++++++++++-- src/core/think.js | 2 +- test/core/base.js | 12 +++++++----- 5 files changed, 57 insertions(+), 15 deletions(-) diff --git a/src/core/app.js b/src/core/app.js index 00f2cb49..eb3321bd 100644 --- a/src/core/app.js +++ b/src/core/app.js @@ -20,11 +20,11 @@ export default class extends think.http.base { let instance = new cls(this.http); let action = think.camelCase(this.http.action); if (think.isFunction(instance[`${action}Action`])) { - return this.action(instance, action); + return this.action(instance, action, false); } //call action else if (think.isFunction(instance.__call)) { - return this.action(instance, '__call'); + return this.action(instance, '__call', false); } //only has before method else if(think.isFunction(instance.__before)){ @@ -71,11 +71,11 @@ export default class extends think.http.base { let actionWithSuffix = `${action}Action`; //action is exist if(think.isFunction(controller[actionWithSuffix])){ - return this.action(controller, action); + return this.action(controller, action, false); } //call action if(think.isFunction(controller.__call)){ - return this.action(controller, '__call'); + return this.action(controller, '__call', false); } http.error = new Error(think.locale('ACTION_NOT_FOUND', actionWithSuffix, http.url)); return think.statusAction(404, http); diff --git a/src/core/base.js b/src/core/base.js index 5d0c953d..7e5bbd96 100644 --- a/src/core/base.js +++ b/src/core/base.js @@ -61,8 +61,24 @@ export default class { * get current class filename * @return {} [] */ - filename(){ - let filename = this.__filename || __filename; - return path.basename(filename, '.js'); + parseFilename(filename){ + filename = filename || this.__filename; + let filenames = filename.split(path.sep).reverse(); + let basename = filenames[0].slice(0, -3); + let module; + switch(think.mode){ + case think.mode_module: + module = filenames[2]; + break; + case think.mode_normal: + module = filenames[1]; + break; + default: + module = think.config('default_module'); + } + return { + module, + basename + }; } } \ No newline at end of file diff --git a/src/core/http_base.js b/src/core/http_base.js index 663fc424..cd3166d0 100644 --- a/src/core/http_base.js +++ b/src/core/http_base.js @@ -32,14 +32,38 @@ export default class extends Base { * @param {Mixed} data [action params] * @return {} [] */ - action(controller, action){ + async action(controller, action, transMCA = true){ if (think.isString(controller)) { controller = this.controller(controller); } + if(!transMCA){ + if (action !== '__call') { + action = think.camelCase(action) + 'Action'; + } + return controller.invoke(action, controller); + } + + //change module/controller/action when invoke another action + //make this.display() correct when invoked without any paramters + let http = this.http; + let source = { + module: http.module, + controller: http.controller, + action: http.action + }; + let ret = this.parseFilename(controller.__filename); + http.module = ret.module; + http.controller = ret.basename; + http.action = action; if (action !== '__call') { action = think.camelCase(action) + 'Action'; } - return controller.invoke(action, controller); + let err; + let result = await controller.invoke(action, controller).catch(e => { + err = e; + }); + think.extend(http, source); + return err ? Promise.reject(err) : result; } /** * get or set cache diff --git a/src/core/think.js b/src/core/think.js index 57a4af1f..570e8431 100644 --- a/src/core/think.js +++ b/src/core/think.js @@ -313,7 +313,7 @@ think.require = (name, flag) => { let filepath = thinkCache(thinkCache.ALIAS, name); if (filepath) { - return load(name, filepath); + return load(name, path.normalize(filepath)); } // only check in alias if (flag) { diff --git a/test/core/base.js b/test/core/base.js index 26337826..8a7130e5 100644 --- a/test/core/base.js +++ b/test/core/base.js @@ -103,14 +103,16 @@ describe('core/base.js', function(){ }) it('no __before & __after', function(done){ var instance = new Cls4(); - instance.invoke('filename', 'getName').then(function(data){ - assert.equal(data, 'base'); + instance.invoke('parseFilename', 'getName.js').then(function(data){ + assert.deepEqual(data.basename, 'getName'); done(); + }).catch(function(err){ + console.log(err.stack) }); }) - it('filename', function(){ + it('parseFilename', function(){ var instance = new Base(); - var filename = instance.filename(); - assert.equal(filename, 'base') + var ret = instance.parseFilename(__filename); + assert.equal(ret.basename, 'base') }) }) \ No newline at end of file