mirror of
https://github.com/thinkjs/thinkjs.git
synced 2026-01-18 14:26:56 +00:00
change module/controller/action when invoke another action
This commit is contained in:
parent
6d7625af2a
commit
a3973e9ff0
@ -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);
|
||||
|
||||
@ -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
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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')
|
||||
})
|
||||
})
|
||||
Loading…
x
Reference in New Issue
Block a user