Skip to content

Commit

Permalink
fixed method passing example
Browse files Browse the repository at this point in the history
  • Loading branch information
sdepold committed May 4, 2011
1 parent 15cb762 commit f1af030
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 64 deletions.
64 changes: 0 additions & 64 deletions examples/MethodPassing/app.js

This file was deleted.

File renamed without changes.
63 changes: 63 additions & 0 deletions examples/method-passing/app.js
@@ -0,0 +1,63 @@
/*
Title: Defining class and instance methods
This example shows the usage of the classMethods and instanceMethods option for Models.
*/

var Sequelize = require(__dirname + "/../../index")
, config = require(__dirname + "/../../test/config")
, sequelize = new Sequelize(config.database, config.username, config.password, {logging: false})

// model definition
var Task = sequelize.define("Task", {
name: Sequelize.STRING,
deadline: Sequelize.DATE,
importance: Sequelize.INTEGER
}, {
classMethods: {
setImportance: function(newImportance, callback) {
Task.findAll().on('success', function(allTasks) {
var chainer = new Sequelize.Utils.QueryChainer
allTasks.forEach(function(task) {
chainer.add(task.updateAttributes({ importance: newImportance }))
})
chainer.run().on('success', function() {
callback && callback()
})
})
}
},
instanceMethods: {
passedDeadline: function() {
return (this.deadline < new Date())
}
}
})

// instance creation
var task1 = Task.build({
name: 'Choose a nice MySQL connector',
deadline: new Date(Date.parse("Jul 8, 2100")),
importance: 10
}),
task2 = Task.build({
name: 'Build the rest',
deadline: new Date(Date.parse("Jul 8, 2005")),
importance: 90
})

Task.sync({force: true}).on('success', function() {
new Sequelize.Utils.QueryChainer([task1.save(), task2.save()]).run().on('success', function() {
console.log("should be false: " + task1.passedDeadline())
console.log("should be true: " + task2.passedDeadline())
console.log("should be 10: " + task1.importance)

Task.setImportance(30, function() {
Task.findAll().on('success', function(tasks) {
tasks.forEach(function(task) {
console.log("should be 30: " + task.importance)
})
})
})
})
})

0 comments on commit f1af030

Please sign in to comment.