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

Commit 574877a4 by Jan Aagaard Meier

fixes #204 thanks to @bpartridge

1 parent fe7703c6
...@@ -51,7 +51,11 @@ module.exports = (function() { ...@@ -51,7 +51,11 @@ module.exports = (function() {
var chainer = new Utils.QueryChainer var chainer = new Utils.QueryChainer
, association = self.__factory.target.associations[self.__factory.associationAccessor] , association = self.__factory.target.associations[self.__factory.associationAccessor]
, foreignIdentifier = association.isSelfAssociation ? association.foreignIdentifier : association.identifier , foreignIdentifier = association.isSelfAssociation ? association.foreignIdentifier : association.identifier
, unassociatedObjects = newAssociations.filter(function(obj) { return !obj.equalsOneOf(oldAssociations) }) , unassociatedObjects = newAssociations.filter(function (obj) {
return !Utils._.find(oldAssociations, function (old) {
return obj.id === old.id
})
})
unassociatedObjects.forEach(function(unassociatedObject) { unassociatedObjects.forEach(function(unassociatedObject) {
var attributes = {} var attributes = {}
...@@ -69,6 +73,20 @@ module.exports = (function() { ...@@ -69,6 +73,20 @@ module.exports = (function() {
}) })
} }
HasManyDoubleLinked.prototype.injectAdder = function(emitterProxy, newAssociation) {
var attributes = {}
, association = this.__factory.target.associations[this.__factory.associationAccessor]
, foreignIdentifier = association.isSelfAssociation ? association.foreignIdentifier : association.identifier;
attributes[this.__factory.identifier] = this.instance.id
attributes[foreignIdentifier] = newAssociation.id
this.__factory.connectorDAO.create(attributes)
.success(function() { emitterProxy.emit('success', newAssociation) })
.error(function(err) { emitterProxy.emit('error', err) })
.on('sql', function(sql) { emitterProxy.emit('sql', sql) })
}
// private // private
var destroyObsoleteAssociations = function(oldAssociations, newAssociations) { var destroyObsoleteAssociations = function(oldAssociations, newAssociations) {
...@@ -77,7 +95,12 @@ module.exports = (function() { ...@@ -77,7 +95,12 @@ module.exports = (function() {
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
var chainer = new Utils.QueryChainer() var chainer = new Utils.QueryChainer()
var foreignIdentifier = self.__factory.target.associations[self.__factory.associationAccessor].identifier var foreignIdentifier = self.__factory.target.associations[self.__factory.associationAccessor].identifier
var obsoleteAssociations = oldAssociations.filter(function(obj) { return !obj.equalsOneOf(newAssociations) }) var obsoleteAssociations = Utils._.filter(oldAssociations, function (old) {
// Return only those old associations that are not found in new
return !Utils._.find(newAssociations, function (obj) {
return obj.id === old.id
})
})
if (obsoleteAssociations.length === 0) { if (obsoleteAssociations.length === 0) {
return emitter.emit('success', null) return emitter.emit('success', null)
......
...@@ -19,15 +19,25 @@ module.exports = (function() { ...@@ -19,15 +19,25 @@ module.exports = (function() {
var self = this var self = this
, options = this.__factory.options , options = this.__factory.options
, chainer = new Utils.QueryChainer() , chainer = new Utils.QueryChainer()
, obsoleteAssociations = oldAssociations.filter(function (old) {
return !Utils._.find(newAssociations, function (obj) {
return obj.id === old.id
})
})
, unassociatedObjects = newAssociations.filter(function (obj) {
return !Utils._.find(oldAssociations, function (old) {
return obj.id === old.id
})
})
// clear the old associations // clear the old associations
oldAssociations.forEach(function(associatedObject) { obsoleteAssociations.forEach(function(associatedObject) {
associatedObject[self.__factory.identifier] = null associatedObject[self.__factory.identifier] = null
chainer.add(associatedObject.save()) chainer.add(associatedObject.save())
}) })
// set the new associations // set the new associations
newAssociations.forEach(function(associatedObject) { unassociatedObjects.forEach(function(associatedObject) {
associatedObject[self.__factory.identifier] = self.instance.id associatedObject[self.__factory.identifier] = self.instance.id
chainer.add(associatedObject.save()) chainer.add(associatedObject.save())
}) })
...@@ -38,5 +48,14 @@ module.exports = (function() { ...@@ -38,5 +48,14 @@ module.exports = (function() {
.error(function(err) { emitter.emit('error', err) }) .error(function(err) { emitter.emit('error', err) })
} }
HasManySingleLinked.prototype.injectAdder = function(emitterProxy, newAssociation) {
newAssociation[this.__factory.identifier] = this.instance.id
newAssociation.save()
.success(function() { emitterProxy.emit('success', newAssociation) })
.error(function(err) { emitterProxy.emit('error', err) })
.on('sql', function(sql) { emitterProxy.emit('sql', sql) })
}
return HasManySingleLinked return HasManySingleLinked
})() })()
...@@ -151,19 +151,18 @@ module.exports = (function() { ...@@ -151,19 +151,18 @@ module.exports = (function() {
obj[this.accessors.add] = function(newAssociatedObject) { obj[this.accessors.add] = function(newAssociatedObject) {
var instance = this var instance = this
var customEventEmitter = new Utils.CustomEventEmitter(function() { return new Utils.CustomEventEmitter(function(emitter) {
instance[self.accessors.get]() instance[self.accessors.get]({ where: { id: newAssociatedObject.id }})
.error(function(err){ customEventEmitter.emit('error', err)}) .error(function(err){ emitter.emit('error', err)})
.success(function(currentAssociatedObjects) { .success(function(currentAssociatedObjects) {
if (!newAssociatedObject.equalsOneOf(currentAssociatedObjects)) if (currentAssociatedObjects.length === 0) {
currentAssociatedObjects.push(newAssociatedObject) var Class = self.connectorDAO ? HasManyMultiLinked : HasManySingleLinked
new Class(self, instance).injectAdder(emitter, newAssociatedObject)
instance[self.accessors.set](currentAssociatedObjects) } else {
.success(function(instances) { customEventEmitter.emit('success', instances) }) emitter.emit('success', newAssociatedObject);
.error(function(err) { customEventEmitter.emit('error', err) }) }
})
}) })
return customEventEmitter.run() }).run()
} }
obj[this.accessors.remove] = function(oldAssociatedObject) { obj[this.accessors.remove] = function(oldAssociatedObject) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!