Skip to content

Commit 63192ff

Browse files
committed
Describe object livecycle, update isValid usage
1 parent c96cf9f commit 63192ff

File tree

1 file changed

+58
-3
lines changed

1 file changed

+58
-3
lines changed

README.md

+58-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Post.create(cb);
6161
// all posts
6262
Post.all(cb)
6363
// all posts by user
64-
Post.all({userId: user.id});
64+
Post.all({where: {userId: user.id}});
6565
// the same as prev
6666
user.posts(cb)
6767
// same as new Post({userId: user.id});
@@ -83,9 +83,64 @@ User.validatesLengthOf('password', {min: 5, message: {min: 'Password is too shor
8383
User.validatesInclusionOf('gender', {in: ['male', 'female']});
8484
User.validatesExclusionOf('domain', {in: ['www', 'billing', 'admin']});
8585
User.validatesNumericalityOf('age', {int: true});
86+
User.validatesUniquenessOf('email', {message: 'email is not unique'});
8687

87-
user.isValid() // false
88-
user.errors // hash of errors {attr: [errmessage, errmessage, ...], attr: ...}
88+
user.isValid(function (valid) {
89+
if (!valid) {
90+
user.errors // hash of errors {attr: [errmessage, errmessage, ...], attr: ...}
91+
}
92+
})
93+
94+
```
95+
96+
## Callbacks
97+
98+
The following callbacks supported:
99+
100+
- afterInitialize
101+
- beforeCreate
102+
- afterCreate
103+
- beforeSave
104+
- afterSave
105+
- beforeUpdate
106+
- afterUpdate
107+
- beforeDestroy
108+
- afterDestroy
109+
- beforeValidation
110+
- afterValidation
111+
112+
Each callback is class method of the model, it should accept single argument: `next`, this is callback which
113+
should be called after end of the hook. Except `afterInitialize` because this method is syncronous (called after `new Model`).
114+
115+
## Object lifecycle:
116+
117+
```javascript
118+
var user = new User;
119+
// afterInitialize
120+
user.save(callback);
121+
// beforeValidation
122+
// afterValidation
123+
// beforeSave
124+
// beforeCreate
125+
// afterCreate
126+
// afterSave
127+
// callback
128+
user.updateAttribute('email', '[email protected]', callback);
129+
// beforeValidation
130+
// afterValidation
131+
// beforeUpdate
132+
// afterUpdate
133+
// callback
134+
user.destroy(callback);
135+
// beforeDestroy
136+
// afterDestroy
137+
// callback
138+
User.create(data, callback);
139+
// beforeValidate
140+
// afterValidate
141+
// beforeCreate
142+
// afterCreate
143+
// callback
89144
```
90145

91146
Read the tests for usage examples: ./test/common_test.js

0 commit comments

Comments
 (0)