From a37129bdff9f6066e061f6ee3445819ef5503f5d Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Tue, 8 Jul 2014 14:04:20 -0700 Subject: [PATCH] Allows default model class to be configured --- lib/model-builder.js | 3 ++- test/loopback-dl.test.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/model-builder.js b/lib/model-builder.js index 89c98d1e..9398a34c 100644 --- a/lib/model-builder.js +++ b/lib/model-builder.js @@ -37,6 +37,7 @@ function ModelBuilder() { // create blank models pool this.models = {}; this.definitions = {}; + this.defaultModelBaseClass = DefaultModelBaseClass; } // Inherit from EventEmitter @@ -131,7 +132,7 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett } // Set up the base model class - var ModelBaseClass = parent || DefaultModelBaseClass; + var ModelBaseClass = parent || this.defaultModelBaseClass; var baseClass = settings.base || settings['super']; if (baseClass) { if (isModelClass(baseClass)) { diff --git a/test/loopback-dl.test.js b/test/loopback-dl.test.js index 3fa1ccd8..31eec3e7 100644 --- a/test/loopback-dl.test.js +++ b/test/loopback-dl.test.js @@ -1285,6 +1285,39 @@ describe('Load models from json', function () { } }); + it('should allow customization of default model base class', function () { + var modelBuilder = new ModelBuilder(); + + var User = modelBuilder.define('User', { + name: String, + bio: ModelBuilder.Text, + approved: Boolean, + joinedAt: Date, + age: Number + }); + + modelBuilder.defaultModelBaseClass = User; + + var Customer = modelBuilder.define('Customer', {customerId: {type: String, id: true}}); + assert(Customer.prototype instanceof User); + }); + + it('should allow model base class', function () { + var modelBuilder = new ModelBuilder(); + + var User = modelBuilder.define('User', { + name: String, + bio: ModelBuilder.Text, + approved: Boolean, + joinedAt: Date, + age: Number + }); + + var Customer = modelBuilder.define('Customer', + {customerId: {type: String, id: true}}, {}, User); + assert(Customer.prototype instanceof User); + }); + it('should be able to extend models', function (done) { var modelBuilder = new ModelBuilder();