Skip to content

Commit

Permalink
pass associations on success to the callback + example
Browse files Browse the repository at this point in the history
  • Loading branch information
sdepold committed May 4, 2011
1 parent 98a5bff commit 00ebd92
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 64 deletions.
10 changes: 0 additions & 10 deletions examples/UsingMultipleModelFiles/Project.js

This file was deleted.

11 changes: 0 additions & 11 deletions examples/UsingMultipleModelFiles/Task.js

This file was deleted.

37 changes: 0 additions & 37 deletions examples/UsingMultipleModelFiles/app.js

This file was deleted.

6 changes: 6 additions & 0 deletions examples/using-multiple-model-files/Project.js
@@ -0,0 +1,6 @@
module.exports = function(sequelize, DataTypes) {
return sequelize.define("Project", {
name: DataTypes.STRING,
description: DataTypes.TEXT
})
}
7 changes: 7 additions & 0 deletions examples/using-multiple-model-files/Task.js
@@ -0,0 +1,7 @@
module.exports = function(sequelize, DataTypes) {
return sequelize.define("Task", {
name: DataTypes.STRING,
deadline: DataTypes.DATE,
importance: DataTypes.INTEGER
})
}
25 changes: 25 additions & 0 deletions examples/using-multiple-model-files/app.js
@@ -0,0 +1,25 @@
var Sequelize = require(__dirname + "/../../index")
, config = require(__dirname + "/../../test/config")
, sequelize = new Sequelize(config.database, config.username, config.password, {logging: false})
, Project = sequelize.import(__dirname + "/Project")
, Task = sequelize.import(__dirname + "/Task")

Project.hasMany(Task)
Task.belongsTo(Project)

sequelize.sync({force: true}).on('success', function() {
Project
.create({ name: 'Sequelize', description: 'A nice MySQL ORM for NodeJS' })
.on('success', function(project) {
Task.create({ name: 'Choose a nice MySQL connector', deadline: new Date(), importance: 10 })
.on('success', function(task1) {
Task.create({ name: 'Build the rest', deadline: new Date(), importance: 90 })
.on('success', function(task2) {
project.setTasks([task1, task2]).on('success', function(tasks) {
console.log(project)
console.log(tasks)
})
})
})
})
})
2 changes: 1 addition & 1 deletion lib/sequelize/associations/has-many-double-linked.js
Expand Up @@ -86,7 +86,7 @@ HasManyDoubleLinked.prototype.injectSetter = function(emitter, oldAssociations,

chainer
.run()
.on('success', function() { emitter.emit('success', null) })
.on('success', function() { emitter.emit('success', newAssociations) })
.on('failure', function(err) { emitter.emit('failure', err) })
})
}
6 changes: 3 additions & 3 deletions lib/sequelize/associations/has-many-single-linked.js
Expand Up @@ -28,7 +28,7 @@ HasManySingleLinked.prototype.injectSetter = function(emitter, oldAssociations,
chainer.add(associatedObject.save())
})
chainer
.run()
.on('success', function() { emitter.emit('success', null) })
.on('failure', function() { emitter.emit('failure', null) })
.run()
.on('success', function() { emitter.emit('success', newAssociations) })
.on('failure', function(err) { emitter.emit('failure', err) })
}
4 changes: 2 additions & 2 deletions lib/sequelize/associations/has-many.js
Expand Up @@ -85,8 +85,8 @@ HasMany.prototype.injectSetter = function(obj) {
currentAssociatedObjects.push(newAssociatedObject)

instance[self.accessors.set](currentAssociatedObjects)
.on('success', function() { customEventEmitter.emit('success', null) })
.on('failure', function() { customEventEmitter.emit('failure', null) })
.on('success', function(instances) { customEventEmitter.emit('success', instances) })
.on('failure', function(err) { customEventEmitter.emit('failure', err) })
})
})
return customEventEmitter.run()
Expand Down

0 comments on commit 00ebd92

Please sign in to comment.