Skip to content

Middleware

Rafał Lorenz edited this page May 7, 2017 · 1 revision

WebComponen decorator uses middleware system to handle options.

Example of such middleware is a function setting template of custom element.

setTemplate.js

export default target => options => {
  const template = createTemplate(options.template);
  if (template) {
    target.appendChild(template);
  }

  return options
}

And here is how middleware is applied to custom elements

createTarget.js

import applyMiddleware from './applyMiddleware'
import setTemplate from './middleware/setTemplate'

export default original => options => {
  return function (...args) {
    let constructor = original.apply(this, args);
    let target = constructor;

    applyMiddleware(setTemplate)(target)(options)

    return constructor;
  }
}

applyMiddleware method accepts middleware functions.

applyMiddleware(mid1, mid2, mid3)

If web-component library is missing something ? Or you want to add some features, simply fork this repository, add middleware and create pull request.

Clone this wiki locally