Docs: hooks, footer fix

This commit is contained in:
Anatoliy Chakkaev 2013-03-27 04:47:34 +04:00
parent 7772c32c80
commit d3752eab4c
2 changed files with 37 additions and 19 deletions

View File

@ -1,13 +1,17 @@
<script> <script>
var filename = location.href.match(/([^\/]+)?\.3.html(#.*?)?$/); var filename = location.href.match(/([^\/]+)?\.3.html(#.*?)?$/);
if (filename) { if (!filename) {
filename = [null, 'jugglingdb'];
}
var div = document.createElement('div'); var div = document.createElement('div');
div.innerHTML = 'Found a typo? ' + div.innerHTML = 'Found a typo? ' +
linkTo('View', 'blob') + ' and ' + linkTo('View', 'blob') +
linkTo('edit', 'edit') + ' this file online at GitHub.'; ' and ' +
'edit' +
//linkTo('edit', 'edit') +
' this file online at GitHub.';
document.getElementById('man').appendChild(div); document.getElementById('man').appendChild(div);
}
function linkTo(text, dir) { function linkTo(text, dir) {
return '<a href="https://github.com/1602/jugglingdb/' + return '<a href="https://github.com/1602/jugglingdb/' +

View File

@ -5,17 +5,29 @@ jugglingdb-hooks(3) - Hooks and object lifecycle.
Hook is a class method called on object when some event happens. List of events: Hook is a class method called on object when some event happens. List of events:
* initialize - called after `new Model` called * `initialize`:
* create - called before and after create Called after `new Model` called.
* update - called before and after save (except create)
* save - called before and after save (including both create and update) * `create`:
* validate - called before and after validations Called before and after create.
* destroy - called before and after destroy on instance
* `update`:
Called before and after save (except create).
* `save`:
Called before and after save (including both create and update).
* `validate`:
Called before and after validations.
* `destroy`:
Called before and after destroy on instance.
Each hook except `initialize` accepts callback as first argument. This callback Each hook except `initialize` accepts callback as first argument. This callback
should be called when hook done. All hooks called on object instance, but it's should be called when hook done. All hooks called on object instance, but it's
not recommended to use `this` for updating in all hooks where data argument not recommended to use `this` for updating in all hooks where data argument
available (second arguments for all data-related before-hooks: save, update, available (second argument for all data-related before-hooks: save, update,
create). create).
## INITIALIZE ## INITIALIZE
@ -32,7 +44,7 @@ being applied.
## CREATE ## CREATE
Create hooks called when object created. Create hooks called when object created.
The `beforeCreate` hook accepts data as second arguments. The `beforeCreate` hook accepts `data` as a second argument.
Model.beforeCreate = function(next, data) { Model.beforeCreate = function(next, data) {
// use data argument to update object // use data argument to update object
@ -61,7 +73,7 @@ Example output will be:
## UPDATE ## UPDATE
Update hooks called on each save except create. Update hooks called on each save except create.
The `beforeUpdate` hook accepts data as second arguments. The `beforeUpdate` hook accepts data as second argument.
Data argument only containing actual data for update, not full object data. Data argument only containing actual data for update, not full object data.
Model.beforeUpdate = function(next, data) { Model.beforeUpdate = function(next, data) {
@ -92,11 +104,13 @@ Example output will be:
## SAVE ## SAVE
Save hooks called on each save, both update and create. Save hooks called on each save, both update and create.
The `beforeSave` hook accepts data as second arguments. The `beforeSave` hook accepts `data` as a second argument.
For before save hook data argument is the same as this. For `beforeSave` hook `data` argument is the same as `this`.
Model.beforeSave = function(next, data) { Model.beforeSave = function(next, data) {
data.tags = JSON.parse(data.tags); if ('string' !== typeof data.tags) {
data.tags = JSON.stringify(data.tags);
}
next(); next();
}; };