1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-28 19:10:28 +00:00
TiddlyWiki5/editions/dev/tiddlers/new/HookMechanism.tid

65 lines
2.4 KiB
Plaintext
Raw Normal View History

2014-11-22 20:06:18 +00:00
created: 20141122200310516
modified: 20230923031318421
tags: Mechanisms
2014-11-25 19:33:12 +00:00
title: HookMechanism
2014-11-22 20:06:18 +00:00
type: text/vnd.tiddlywiki
The hook mechanism provides a way for plugins to intercept and modify default functionality.
!! Add a hook
Hooks are added as follows:
2014-11-22 20:06:18 +00:00
2014-11-28 10:06:17 +00:00
```js
2020-12-13 20:01:47 +00:00
/*
name: name of hook function (by convention prefixed with `th-`)
handler: function to be called when hook is invoked
*/
2014-11-28 10:06:17 +00:00
$tw.hooks.addHook(name,handler);
```
2014-11-22 20:06:18 +00:00
!!! Params and return
2020-12-13 20:01:47 +00:00
The handler function will be called with parameters that depend on the specific hook in question, but they always follow the pattern `handler(value,params...)`
* ''value'': an optional value that is to be transformed by the hook function
* ''params'': one or more optional parameters that are passed to the hook function
If required by the hook in question, the handler function must return the modified ''value''.
!!! Multiple handlers
2014-11-25 19:33:12 +00:00
Multiple handlers can be assigned to the same name using repeated calls. When a hook is invoked by name all registered functions will be called sequentially in their order of addition.
2014-11-22 20:06:18 +00:00
2020-12-13 20:01:47 +00:00
Note that the ''value'' passed to the subsequent hook function will be the return value of the previous hook function.
Be careful not to `addHook` in widget's `render` method, which will be call several times. You could `addHook` in methods that only called once, e.g. the constructor of widget class. Otherwise you should `removeHook` then add it again.
!!! Timing of registration
Though not essential care should be taken to ensure that hooks are added before they are invoked.
For example: [[Hook: th-opening-default-tiddlers-list]] should ideally be added before the story startup module is invoked. Otherwise any hook specified additions to the default tiddlers will not be seen on the initial loading of the page, though will be visible if the user clicks the home button.
!! Remove a hook
You should clean up the callback when your widget is going to unmount.
```js
$tw.hooks.removeHook(handler)
```
The `handler` should be the same function instance you used in `addHook` (check by `===`). You can save it to `this.xxxHookHandler` on your widget, and call `removeHook` in [[destroy method|Widget `destroy` method examples]].
2014-11-22 20:06:18 +00:00
2014-11-25 19:33:12 +00:00
!! Example
2014-11-22 20:06:18 +00:00
2014-11-25 19:33:12 +00:00
A working example of a hook that adds "test" to the default tiddlers.
2014-11-22 20:06:18 +00:00
2014-11-28 10:06:17 +00:00
```js
2014-11-22 20:06:18 +00:00
$tw.hooks.addHook("th-opening-default-tiddlers-list",function(list) {
list.push("test");
return list;
});
```