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

Commit be4e58ea by Sascha Depold

refactored hasMany; split into multiple files for single and double linked associations

1 parent 35f9f304
var Utils = require('./../utils')
var HasManyDoubleLinked = module.exports = function(definition, instance) {
this.definition = definition
this.instance = instance
}
HasManyDoubleLinked.prototype.injectGetter = function() {
var self = this
var customEventEmitter = new Utils.CustomEventEmitter(function() {
var where = {}
where[self.definition.identifier] = self.instance.id
self.definition.connectorModel.findAll({where: where}).on('success', function(associatedObjects) {
customEventEmitter.emit('success', associatedObjects)
})
})
return customEventEmitter.run()
}
HasManyDoubleLinked.prototype.injectSetter = function(emitter, oldAssociations, newAssociations) {
var self = this
// destroy the old association objects
var foreignIdentifier = self.definition.target.associations[self.definition.source.tableName].identifier
var destroyChainer = new Utils.QueryChainer
oldAssociations.forEach(function(associatedObject) { destroyChainer.add(associatedObject.destroy()) })
destroyChainer
.run()
.on('failure', function(err) { emitter.emit('failure', err) })
.on('success', function() {
// create new one
var createChainer = new Utils.QueryChainer
newAssociations.forEach(function(associatedObject) {
var attributes = {}
attributes[self.definition.identifier] = self.instance.id
attributes[foreignIdentifier] = associatedObject.id
createChainer.add(self.definition.connectorModel.create(attributes))
})
createChainer
.run()
.on('success', function() { emitter.emit('success', null) })
.on('failure', function(err) { emitter.emit('failure', err) })
})
}
\ No newline at end of file
var Utils = require('./../utils')
var HasManySingleLinked = module.exports = function(definition, instance) {
this.definition = definition
this.instance = instance
}
HasManySingleLinked.prototype.injectGetter = function() {
var where = {}
where[this.definition.identifier] = this.instance.id
return this.definition.target.findAll({where: where})
}
HasManySingleLinked.prototype.injectSetter = function(emitter, oldAssociations, newAssociations) {
var self = this
// clear the old associations
oldAssociations.forEach(function(associatedObject) {
associatedObject[self.definition.identifier] = null
associatedObject.save()
})
// set the new one
var chainer = new Utils.QueryChainer
newAssociations.forEach(function(associatedObject) {
associatedObject[self.definition.identifier] = self.instance.id
chainer.add(associatedObject.save())
})
chainer
.run()
.on('success', function() { emitter.emit('success', null) })
.on('failure', function() { emitter.emit('failure', null) })
}
\ No newline at end of file
var Utils = require("./../utils")
, DataTypes = require('./../data-types')
var HasManySingleLinked = require("./has-many-single-linked")
, HasManyMultiLinked = require("./has-many-double-linked")
var HasMany = module.exports = function(srcModel, targetModel, options) {
this.source = srcModel
this.target = targetModel
......@@ -46,23 +49,8 @@ HasMany.prototype.injectGetter = function(obj) {
var self = this
obj[this.accessors.get] = function() {
var instance = this
if(self.connectorModel) {
var customEventEmitter = new Utils.CustomEventEmitter(function() {
var where = {}
where[self.identifier] = instance.id
self.connectorModel.findAll({where: where}).on('success', function(associatedObjects) {
customEventEmitter.emit('success', associatedObjects)
})
})
return customEventEmitter.run()
} else {
var where = {}
where[self.identifier] = this.id
return self.target.findAll({where: where})
}
var Class = self.connectorModel ? HasManyMultiLinked : HasManySingleLinked
return new Class(self, this).injectGetter()
}
return this
......@@ -77,50 +65,8 @@ HasMany.prototype.injectSetter = function(obj) {
// define the returned customEventEmitter, which will emit the success event once everything is done
var customEventEmitter = new Utils.CustomEventEmitter(function() {
instance[self.accessors.get]().on('success', function(oldAssociatedObjects) {
if(self.connectorModel) {
var foreignIdentifier = self.target.associations[self.source.tableName].identifier
var destroyChainer = new Utils.QueryChainer
// destroy the old association objects
oldAssociatedObjects.forEach(function(associatedObject) { destroyChainer.add(associatedObject.destroy()) })
destroyChainer
.run()
.on('failure', function(err) { customEventEmitter.emit('failure', err) })
.on('success', function() {
// create new one
var createChainer = new Utils.QueryChainer
newAssociatedObjects.forEach(function(associatedObject) {
var attributes = {}
attributes[self.identifier] = instance.id
attributes[foreignIdentifier] = associatedObject.id
createChainer.add(self.connectorModel.create(attributes))
})
createChainer
.run()
.on('success', function() { customEventEmitter.emit('success', null) })
.on('failure', function(err) { customEventEmitter.emit('failure', err) })
})
} else {
// clear the old associations
oldAssociatedObjects.forEach(function(associatedObject) {
associatedObject[self.identifier] = null
associatedObject.save()
})
// set the new one
var chainer = new Utils.QueryChainer
newAssociatedObjects.forEach(function(associatedObject) {
associatedObject[self.identifier] = instance.id
chainer.add(associatedObject.save())
})
chainer
.run()
.on('success', function() { customEventEmitter.emit('success', null) })
.on('failure', function() { customEventEmitter.emit('failure', null) })
}
var Class = self.connectorModel ? HasManyMultiLinked : HasManySingleLinked
new Class(self, instance).injectSetter(customEventEmitter, oldAssociatedObjects, newAssociatedObjects)
})
})
return customEventEmitter.run()
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!