fix #429 Multiple Models can't mixin same class

Signed-off-by: Clark Wang <clark.wangs@gmail.com>
This commit is contained in:
Clark Wang 2015-03-11 16:02:00 +08:00
parent 159be756ac
commit 90e169c1a6
2 changed files with 51 additions and 17 deletions

View File

@ -1,4 +1,5 @@
var util = require('util'); var util = require('util');
var _ = require('lodash');
/** /**
* *
* @param newClass * @param newClass
@ -75,11 +76,12 @@ function mixInto(sourceScope, targetScope, options) {
var isDelegate = isFunc && targetProperty.value._delegate; var isDelegate = isFunc && targetProperty.value._delegate;
var shouldOverride = options.override || !targetPropertyExists || isDelegate; var shouldOverride = options.override || !targetPropertyExists || isDelegate;
if (shouldOverride) { if (propertyName == '_mixins') {
if (sourceIsFunc) { targetScope._mixins = _.union(targetScope._mixins, sourceScope._mixins);
sourceProperty.value = sourceProperty.value; return;
} }
if (shouldOverride) {
Object.defineProperty(targetScope, propertyName, sourceProperty); Object.defineProperty(targetScope, propertyName, sourceProperty);
} }
}); });

View File

@ -105,4 +105,36 @@ describe('Model class', function () {
}); });
}); });
describe('#mixin()', function () {
var Person, Author, Address;
beforeEach(function () {
Address = modelBuilder.define('Address', {
street: { type: 'string', required: true },
city: { type: 'string', required: true }
});
var memory = new DataSource('mem', {connector: Memory}, modelBuilder);
Person = memory.createModel('Person', { name: 'string' });
Author = memory.createModel('Author', { name: 'string' });
});
it('should register mixin class into _mixins', function () {
Person.mixin(Address);
Person._mixins.should.containEql(Address);
});
it('should NOT share mixins registry', function () {
Person.mixin(Address);
Author._mixins.should.not.containEql(Address);
});
it('should able to mixin same class', function () {
Person.mixin(Address);
Author.mixin(Address);
Author._mixins.should.containEql(Address);
});
});
}); });