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

Commit 7f53f984 by Sascha Depold

correctly disconnecting associated objects from the source when associating with a new one

1 parent fea413f1
......@@ -5,6 +5,10 @@ var HasOne = module.exports = function(srcModel, targetModel, options) {
this.source = srcModel
this.target = targetModel
this.options = options
this.accessors = {
get: Utils._.camelize('get_' + (this.options.as || this.target.tableName)),
set: Utils._.camelize('set_' + (this.options.as || this.target.tableName))
}
}
// the id is in the target table
......@@ -14,15 +18,14 @@ HasOne.prototype.injectAttributes = function() {
this.identifier = this.options.foreignKey || Utils._.underscoredIf(this.source.tableName + "Id", this.options.underscored)
newAttributes[this.identifier] = { type: DataTypes.INTEGER }
Utils._.extend(this.target.attributes, Utils.simplifyAttributes(newAttributes))
return this
}
HasOne.prototype.injectGetter = function(obj) {
var self = this
, accessor = Utils._.camelize('get_' + (this.options.as || this.target.tableName))
var self = this
obj[accessor] = function() {
obj[this.accessors.get] = function() {
var id = obj.id
, where = {}
......@@ -34,12 +37,25 @@ HasOne.prototype.injectGetter = function(obj) {
}
HasOne.prototype.injectSetter = function(obj) {
var self = this
, accessor = Utils._.camelize('set_' + (this.options.as || this.target.tableName))
var self = this
obj[accessor] = function(associatedObject) {
associatedObject[self.identifier] = this.id
return associatedObject.save()
obj[this.accessors.set] = function(associatedObject) {
var customEventEmitter = new Utils.CustomEventEmitter(function() {
obj[self.accessors.get]().on('success', function(oldObj) {
if(oldObj) {
oldObj[self.identifier] = null
oldObj.save()
}
associatedObject[self.identifier] = obj.id
associatedObject.save().on('success', function() {
customEventEmitter.emit('success', '')
}).on('failure', function(err) {
customEventEmitter.emit('failure', err)
})
})
})
return customEventEmitter
}
return this
......
......@@ -120,4 +120,7 @@ var Utils = module.exports = {
})
return result
}
}
\ No newline at end of file
}
Utils.CustomEventEmitter = function(fct) { fct() }
Utils.addEventEmitter(Utils.CustomEventEmitter)
\ No newline at end of file
......@@ -70,5 +70,33 @@ module.exports = {
})
})
})
},
'it should correctly unset the obsolete objects': function(exit) {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
User.hasOne(Task, {as: 'Task'})
User.sync({force: true}).on('success', function() {
Task.sync({force: true}).on('success', function() {
User.create({username: 'name'}).on('success', function(user) {
Task.create({title: 'snafu'}).on('success', function(task) {
Task.create({title: 'another task'}).on('success', function(task2) {
user.setTask(task).on('success', function() {
user.getTask().on('success', function(_task) {
assert.eql(task.title, _task.title)
user.setTask(task2).on('success', function() {
user.getTask().on('success', function(_task2) {
assert.eql(task2.title, _task2.title)
exit(function(){})
})
})
})
})
})
})
})
})
})
}
}
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!