From 14998bff45f1fd344819c1f5b4f33eb57c4680af Mon Sep 17 00:00:00 2001 From: lichengyin Date: Tue, 8 Sep 2015 20:29:43 +0800 Subject: [PATCH] set _module property when get model instance --- src/core/think.js | 5 ++++- src/model/_base.js | 2 +- test/core/think.js | 8 ++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/core/think.js b/src/core/think.js index b5c1596d..b0a6d09e 100644 --- a/src/core/think.js +++ b/src/core/think.js @@ -1098,7 +1098,10 @@ think.model = (superClass, methods, module) => { methods = think.extend({}, think.config('db'), methods); let base = methods.type === 'mongo' ? 'model_mongo' : ''; let cls = think.lookClass(superClass, 'model', module, base); - return new cls(superClass, methods); + let instance = new cls(superClass, methods); + //set module for model method used + instance._module = module; + return instance; } let model = thinkCache(thinkCache.COLLECTION, 'model'); if(!model){ diff --git a/src/model/_base.js b/src/model/_base.js index 7e1e71d1..45704fe1 100644 --- a/src/model/_base.js +++ b/src/model/_base.js @@ -83,7 +83,7 @@ export default class { */ model(name, options){ options = think.extend({}, this.config, options); - return think.model(name, options); + return think.model(name, options, this._module); } /** * get config key diff --git a/test/core/think.js b/test/core/think.js index 5f1b9485..5b82659d 100644 --- a/test/core/think.js +++ b/test/core/think.js @@ -1999,6 +1999,14 @@ describe('core/think.js', function(){ }) assert.equal(instance.tablePrefix, 'think_'); }) + it('get model instance, _module', function(){ + var instance = think.model('test', { + host: '127.0.0.1', + type: 'mysql' + }, 'home') + assert.equal(instance.tablePrefix, 'think_'); + assert.equal(instance._module, 'home'); + }) it('get model instance, mongo', function(){ var instance = think.model('test', { host: '127.0.0.1',