ioncakephper / dispatcher

Dispatch hooks of a hookable method to methods named automatically

Repository from Github https://github.comioncakephper/dispatcherRepository from Github https://github.comioncakephper/dispatcher

dispatcher

Dispatch hooks of a hook-able method to methods named automatically.

Badges

MIT License

Usage

const { Dispatcher } = require("dispatcher");

class Example {

    find(hook) {
        let d = new Dispatcher();
        return d.dispatch(this, 'find', arguments)
    }

    // method for hook 'all'
    findAll(options = {}) {
        // pass options to a db query
        // return DB.query(options)
        return [{}]
    }

    // default method for hook-able find
    defaultFind(hook, ...args) {
        // return a default answer based on hook and available arguments
    }
}

// in your code:
let options = {}

let m = new Example();
let records = m.find('all', options) // will invoke findAll(options)

// when no hook method exists for a hook, the defaultFind is executed.
let r = m.find("first", options)

About

Dispatch hooks of a hookable method to methods named automatically

License:MIT License


Languages

Language:JavaScript 100.0%