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

Commit 832fa070 by Sascha Depold

added query chainer to wait for multiple queries

1 parent d5377000
var Utils = require("./../utils")
, DataTypes = require('./../data-types')
, QueryChainer = require("./../query-chainer")
var HasMany = module.exports = function(srcModel, targetModel, options) {
this.source = srcModel
......@@ -63,12 +64,12 @@ HasMany.prototype.injectGetter = function(obj) {
HasMany.prototype.injectSetter = function(obj) {
var self = this
obj[this.accessors.set] = function(newAssociatedObject) {
obj[this.accessors.set] = function(newAssociatedObjects) {
var instance = this
// define the returned customEventEmitter, which will emit the success event once everything is done
var customEventEmitter = new Utils.CustomEventEmitter(function() {
self[self.accessors.get]().on('success', function(oldAssociatedObjects) {
instance[self.accessors.get]().on('success', function(oldAssociatedObjects) {
if(self.connectorModel) {
// destroy the old association objects
oldAssociatedObjects.forEach(function(associatedObject) {
......@@ -76,7 +77,7 @@ HasMany.prototype.injectSetter = function(obj) {
})
// create new one
newAssociatedObject.forEach(function(associatedObject) {
newAssociatedObjects.forEach(function(associatedObject) {
var attributes = {}
attributes[self.identifier] = instance.id
attributes[self.foreignIdentifier] = associatedObject.id
......@@ -91,11 +92,14 @@ HasMany.prototype.injectSetter = function(obj) {
})
// set the new one
newAssociatedObject.forEach(function(associatedObject) {
var chainer = new QueryChainer
newAssociatedObjects.forEach(function(associatedObject) {
associatedObject[self.identifier] = instance.id
associatedObject.save()
chainer.add(associatedObject.save())
})
customEventEmitter.emit('success', null)
chainer.run()
.on('success', function() { customEventEmitter.emit('success', null) })
.on('failure', function() { customEventEmitter.emit('failure', null) })
}
})
})
......
var Utils = require("./utils")
var QueryChainer = module.exports = function(emitters) {
var self = this
this.finishedEmits = 0
this.emitters = []
this.fails = []
this.finished = false
this.runned = false
this.instance = null
emitters = emitters || []
emitters.forEach(function(emitter) { self.add(emitter) })
}
Utils.addEventEmitter(QueryChainer)
QueryChainer.prototype.add = function(emitter) {
this.observeEmitter(emitter)
this.emitters.push(emitter)
}
QueryChainer.prototype.observeEmitter = function(emitter) {
var self = this
emitter
.on('success', function(){ self.finishedEmits++; self.finish() })
.on('failure', function(){ self.finishedEmits++; self.fails.push(emitter); self.finish() })
}
QueryChainer.prototype.finish = function(result) {
this.finished = (this.finishedEmits == this.emitters.length)
if(this.finished && this.runned) {
this.instance.emit(this.fails.length == 0 ? 'success' : 'failure', result)
}
}
QueryChainer.prototype.run = function() {
var self = this
this.instance = new Utils.CustomEventEmitter(function() {
self.runned = true
self.finish()
})
return this.instance
}
\ No newline at end of file
......@@ -130,26 +130,32 @@ module.exports = {
var t = Task.build({title: 'asd'})
assert.isDefined(t.setUsers)
assert.isDefined(t.getUsers)
}/*,
'it should set and get the correct objects': function(exit) {
},
'it should set and get the correct objects - monodirectional': 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.hasMany(Task, {as: 'Tasks'})
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) {
user.setTask(task).on('success', function() {
user.getTask().on('success', function(task2) {
assert.eql(task.title, task2.title)
Task.create({title: 'task1'}).on('success', function(task1) {
Task.create({title: 'task2'}).on('success', function(task2) {
user.setTasks([task1, task2]).on('success', function() {
user.getTasks().on('success', function(tasks) {
assert.eql(tasks.length, 2)
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!