From 14d178bf69ff5210b6166bb7158ad1a5b6a213f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Fri, 10 Oct 2014 13:42:59 +0200 Subject: [PATCH] Don't inherit settings.base when extending a model Fix the bug in `ModelClass.extend` where the `base` option used in the new class was inherited from ModelClass. As a result the extended model was incorrectly based on ModelClass's parent. Modify `modelBuilder.define` to normalize the property name storing the name of the base model to `settings.base`. --- lib/model-builder.js | 11 +++++++++++ test/model-definition.test.js | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/model-builder.js b/lib/model-builder.js index 5962a1a0e..87eed664c 100644 --- a/lib/model-builder.js +++ b/lib/model-builder.js @@ -140,6 +140,10 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett var ModelBaseClass = parent || this.defaultModelBaseClass; var baseClass = settings.base || settings['super']; if (baseClass) { + // Normalize base model property + settings.base = baseClass; + delete settings['super']; + if (isModelClass(baseClass)) { ModelBaseClass = baseClass; } else { @@ -337,8 +341,15 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett } // Merge the settings + var originalSubclassSettings = subclassSettings; subclassSettings = mergeSettings(settings, subclassSettings); + // Ensure 'base' is not inherited. Note we don't have to delete 'super' + // as that is removed from settings by modelBuilder.define and thus + // it is never inherited + if (!originalSubclassSettings.base) + delete subclassSettings.base; + // Define the subclass var subClass = modelBuilder.define(className, subclassProperties, subclassSettings, ModelClass); diff --git a/test/model-definition.test.js b/test/model-definition.test.js index e7f22ff1f..342dfde3e 100644 --- a/test/model-definition.test.js +++ b/test/model-definition.test.js @@ -250,6 +250,26 @@ describe('ModelDefinition class', function () { assert(anotherChild.prototype instanceof baseChild); }); + it('should ignore inherited options.base', function() { + var memory = new DataSource({connector: Memory}); + var modelBuilder = memory.modelBuilder; + var base = modelBuilder.define('base'); + var child = base.extend('child', {}, { base: 'base' }); + var grandChild = child.extend('grand-child'); + assert.equal('child', grandChild.base.modelName); + assert(grandChild.prototype instanceof child); + }); + + it('should ignore inherited options.super', function() { + var memory = new DataSource({connector: Memory}); + var modelBuilder = memory.modelBuilder; + var base = modelBuilder.define('base'); + var child = base.extend('child', {}, { super: 'base' }); + var grandChild = child.extend('grand-child'); + assert.equal('child', grandChild.base.modelName); + assert(grandChild.prototype instanceof child); + }); + it('should not serialize hidden properties into JSON', function () { var memory = new DataSource({connector: Memory}); var modelBuilder = memory.modelBuilder;