Permalink
Browse files

fixed method passing example

  • Loading branch information...
1 parent 15cb762 commit f1af030afc28df26594179fedcd7d4b19ba68e2c @sdepold committed May 4, 2011
@@ -1,64 +0,0 @@
-var Sequelize = require(__dirname + "/../../lib/sequelize/Sequelize").Sequelize,
- sequelize = new Sequelize("sequelize_test", "test", "test", {disableLogging: true})
-
-// model definition
-var Task = sequelize.define("Task", {
- name: Sequelize.STRING,
- deadline: Sequelize.DATE,
- importance: Sequelize.INTEGER
-}, {
- classMethods: {
- setImportance: function(newImportance, callback) {
- Task.findAll(function(allTasks) {
- var queries = []
- allTasks.forEach(function(task) {
- queries.push({updateAttributes: task, params: [{ importance: newImportance }]})
- })
- Sequelize.chainQueries(queries, callback)
- })
- }
- },
- instanceMethods: {
- hasDeadlinePassed: function() {
- return (this.deadline < new Date())
- }
- }
-})
-
-// instance creation
-var task1 = new Task({
- name: 'Choose a nice MySQL connector',
- deadline: new Date(Date.parse("Jul 8, 2100")),
- importance: 10
- }),
- task2 = new Task({
- name: 'Build the rest',
- deadline: new Date(Date.parse("Jul 8, 2005")),
- importance: 90
- })
-
-Task.drop(function(table, error) {
- if(error) return Sequelize.Helper.log(error)
-
- Task.sync(function(table, error) {
- if(error) return Sequelize.Helper.log(error)
-
- task1.save(function() {
- task2.save(function() {
-
- Sequelize.Helper.log("should be false: " + task1.hasDeadlinePassed())
- Sequelize.Helper.log("should be true: " + task2.hasDeadlinePassed())
- Sequelize.Helper.log("should be 10: " + task1.importance)
-
- Task.setImportance(30, function() {
- Task.findAll(function(tasks) {
- tasks.forEach(function(task) {
- Sequelize.Helper.log("should be 30: " + task.importance)
- })
- })
- })
-
- })
- })
- })
-})
File renamed without changes.
@@ -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.