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

Commit 5ce0370e by sdepold

clearing for hasOne associations

1 parent 5077d4ac
...@@ -47,23 +47,29 @@ module.exports = (function() { ...@@ -47,23 +47,29 @@ module.exports = (function() {
} }
HasOne.prototype.injectSetter = function(obj) { HasOne.prototype.injectSetter = function(obj) {
var self = this var self = this
, options = self.options || {}
obj[this.accessors.set] = function(associatedObject) { obj[this.accessors.set] = function(associatedObject) {
var customEventEmitter = new Utils.CustomEventEmitter(function() { return new Utils.CustomEventEmitter(function(emitter) {
obj[self.accessors.get]().success(function(oldObj) { obj[self.accessors.get]().success(function(oldObj) {
if(oldObj) { if(oldObj) {
oldObj[self.identifier] = self.options.omitNull ? '' : null; oldObj[self.identifier] = options.omitNull ? '' : null;
oldObj.save() oldObj.save()
} }
associatedObject[self.identifier] = obj.id if(associatedObject) {
associatedObject.save() associatedObject[self.identifier] = obj.id
.success(function() { customEventEmitter.emit('success', associatedObject) }) associatedObject
.error(function(err) { customEventEmitter.emit('error', err) }) .save()
.success(function() { emitter.emit('success', associatedObject) })
.error(function(err) { emitter.emit('error', err) })
} else {
emitter.emit('success', null)
}
}) })
}) }).run()
return customEventEmitter.run()
} }
return this return this
......
...@@ -23,7 +23,7 @@ describe('BelongsTo', function() { ...@@ -23,7 +23,7 @@ describe('BelongsTo', function() {
Task.create({ title: 'task' }).success(function(task) { Task.create({ title: 'task' }).success(function(task) {
task.setUser(user).success(function() { task.setUser(user).success(function() {
task.getUser().success(function(user) { task.getUser().success(function(user) {
expect(user).toBeDefined() expect(user).not.toEqual(null)
task.setUser(null).success(function() { task.setUser(null).success(function() {
task.getUser().success(function(user) { task.getUser().success(function(user) {
......
...@@ -177,8 +177,6 @@ describe('HasMany', function() { ...@@ -177,8 +177,6 @@ describe('HasMany', function() {
}) })
it("adds three items to the query chainer when calling sync", function() { it("adds three items to the query chainer when calling sync", function() {
sequelize.daoFactoryManager.daos = []
var User = sequelize.define('User', { username: Sequelize.STRING }) var User = sequelize.define('User', { username: Sequelize.STRING })
, Task = sequelize.define('Task', { title: Sequelize.STRING }) , Task = sequelize.define('Task', { title: Sequelize.STRING })
......
if (typeof require === 'function') {
const buster = require("buster")
, Sequelize = require("../../index")
, config = require("../config/config")
, sequelize = new Sequelize(config.database, config.username, config.password, {
logging: false
})
}
buster.spec.expose()
buster.testRunner.timeout = 500
describe('HasOne', function() {
before(function(done) {
var self = this
sequelize.getQueryInterface()
.dropAllTables()
.success(function() {
sequelize.daoFactoryManager.daos = []
done()
})
.error(function(err) { console.log(err) })
})
describe('setAssociation', function() {
it('clears the association if null is passed', function(done) {
var User = sequelize.define('User', { username: Sequelize.STRING })
, Task = sequelize.define('Task', { title: Sequelize.STRING })
User.hasOne(Task)
sequelize.sync({ force: true }).success(function() {
User.create({ username: 'foo' }).success(function(user) {
Task.create({ title: 'task' }).success(function(task) {
user.setTask(task).success(function() {
user.getTask().success(function(task) {
expect(task).not.toEqual(null)
user.setTask(null).success(function() {
user.getTask().success(function(task) {
expect(task).toEqual(null)
done()
})
})
})
})
})
})
})
})
})
})
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!