不要怂,就是干,撸起袖子干!

Commit a814e7fa by Jan Aagaard Meier

Made it possible to override instance and class methods. Closes #1359

1 parent a1c2f6a2
Showing with 42 additions and 6 deletions
...@@ -214,7 +214,7 @@ module.exports = (function() { ...@@ -214,7 +214,7 @@ module.exports = (function() {
Utils._(['classMethods', 'instanceMethods']).each(function(key) { Utils._(['classMethods', 'instanceMethods']).each(function(key) {
if (globalOptions.define[key]) { if (globalOptions.define[key]) {
options[key] = options[key] || {} options[key] = options[key] || {}
Utils._.extend(options[key], globalOptions.define[key]) Utils._.defaults(options[key], globalOptions.define[key])
} }
}) })
} }
......
...@@ -408,21 +408,57 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () { ...@@ -408,21 +408,57 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () {
done() done()
}) })
it("inherits global classMethods and instanceMethods", function(done) { it("inherits global classMethods and instanceMethods, and can override global methods with local ones", function(done) {
var sequelize = Support.createSequelizeInstance({ var globalClassMethod = sinon.spy()
, globalInstanceMethod = sinon.spy()
, localClassMethod = sinon.spy()
, localInstanceMethod = sinon.spy()
, sequelize = Support.createSequelizeInstance({
define: { define: {
classMethods : { globalClassMethod : function() {} }, classMethods : {
instanceMethods : { globalInstanceMethod : function() {} } globalClassMethod : function() {},
overrideMe: globalClassMethod
},
instanceMethods : {
globalInstanceMethod : function() {},
overrideMe: globalInstanceMethod
}
} }
}) })
, DAO
var DAO = sequelize.define('foo', {bar: DataTypes.STRING}, { DAO = sequelize.define('foo', {bar: DataTypes.STRING}, {
classMethods : { localClassMethod : function() {} } classMethods : { localClassMethod : function() {} }
}) })
expect(typeof DAO.options.classMethods.globalClassMethod).to.equal('function') expect(typeof DAO.options.classMethods.globalClassMethod).to.equal('function')
expect(typeof DAO.options.classMethods.localClassMethod).to.equal('function') expect(typeof DAO.options.classMethods.localClassMethod).to.equal('function')
expect(typeof DAO.options.instanceMethods.globalInstanceMethod).to.equal('function') expect(typeof DAO.options.instanceMethods.globalInstanceMethod).to.equal('function')
// This DAO inherits the global methods
DAO.overrideMe()
DAO.build().overrideMe()
DAO = sequelize.define('foo', {bar: DataTypes.STRING}, {
classMethods : {
overrideMe : localClassMethod
},
instanceMethods: {
overrideMe: localInstanceMethod
}
})
// This DAO has its own implementation
DAO.overrideMe()
DAO.build().overrideMe()
expect(globalClassMethod).to.have.been.calledOnce
expect(globalInstanceMethod).to.have.been.calledOnce
expect(localClassMethod).to.have.been.calledOnce
expect(localInstanceMethod).to.have.been.calledOnce
done() done()
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!