2
0
mirror of https://github.com/frappe/books.git synced 2024-11-14 09:24:04 +00:00
books/docs/utilities/observable.md
2018-02-22 13:07:50 +05:30

773 B

Observable Base Class

The Observable base class makes and subclass trigger events and accept event listeners.

Example

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