2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00
books/docs/utilities/observable.md

29 lines
773 B
Markdown
Raw Normal View History

2018-02-22 07:36:28 +00:00
# Observable Base Class
The `Observable` base class makes and subclass trigger events and accept event listeners.
### Example
```js
class Test extends Observable {
doSomething() {
// work
this.trigger('work-done', {some: params})
}
}
let test = new Test();
test.on('work-done', (params) => yay());
```
### With Sockets
You can also bind sockets (SocketIO) to an `Observable` and all events will also be emitted or received via the socket. See the API below for binding sockets.
### Methods
- `on(event, listener)`: Listen to an event
- `trigger(event)`: trigger an event
- `once(event, listener)`: trigger an event once
- `bindSocketServer(socket)`: Emit triggers on this socket
- `bindSocketClient(socket)`: Listen for events on this socket