MethodPassing.ejs
2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<script type="text/javascript" charset="utf-8">
document.observe("dom:loaded", function() {
buildNavigation([], ["<br />", "<a href=\"/examples/Count\">Count</a>",
"<a href=\"/examples/DefaultValues\">Default values</a>",
"<a href=\"/examples/fetchAssociations\">fetchAssociations</a>",
"<a href=\"/examples/MethodPassing\">MethodPassing</a>",
"<a href=\"/examples/Performance\">Performance</a>",
"<a href=\"/examples/SequelizeWithOptions\">SequelizeWithOptions</a>",
"<a href=\"/examples/ChainQueries\">Using the chainQueries function</a>",
"<a href=\"/examples/UsingMultipleModelFiles\">UsingMultipleModelFiles</a>",
"<a href=\"/examples/Associations\">Working with associations</a>"], { seperator: ' ' })
})
</script>
<div>
<a name="sequelize"></a>
<h1>MethodPassing</h1>
<p></p>
</div>
<div class="seperator"></div>
<div>
<a name="app.js"></a>
<h2>app.js</h2>
<p>
<pre>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)
})
})
})
})
})
})
})</pre>
</p>
</div>