fix #429 Multiple Models can't mixin same class
Signed-off-by: Clark Wang <clark.wangs@gmail.com>
This commit is contained in:
parent
159be756ac
commit
90e169c1a6
10
lib/jutil.js
10
lib/jutil.js
|
@ -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);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -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);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue