Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

domodule

firstandthird1.9kMIT8.2.1TypeScript support: included

Class-based JavaScript modules accessible via the DOM.

module, browser, javascript, typescript

readme

domodule

Test Status Lint Status NPM Version

Domodule is a helper that allows you to create JavaScript modules with minimal effort while keeping code size down. It automatically binds to elements using the data-module attribute.

Installation

npm install domodule

or

yarn add domodule

Example usage

<div data-module="ExampleModule" data-module-title="Example Module">
  <div data-name="title"></div>
  <button type="button" data-action="click">Show Title</button>
</div>
class ExampleModule extends Domodule {
  click() {
    this.els.title.textContent = this.options.title;
  }
}

Inherited methods

Each module has access to these helper methods.

  • find(<selector>) - Returns an array of matched elements inside of the module.
  • findOne(<selector>) - Returns the first matched element inside of the module.
  • findByName(<element name>) - Alternative to this.els[name].
  • getOption(<option>) - Returns value of an option (data-module-*).
  • setupActions() - Used to bind actions. Useful if the module adds elements in after Domodule inits. Note: Called by default. Calling again wont re-process elements.
  • setupNamed() - Same as setupActions() but binds to named elements. Note: Called by default. Calling again wont re-process elements.

Static Methods

  • Domodule.getInstance(<element>) - Returns an instance of the module.
  • Domodule.discover(<dom node, array of nodes, selector>) - Looks for data-module inside of matched elements. Will skip elements already processed. Calling just Domodule.discover() will search for all modules in the body.

Named elements

Adding data-name=<name> to an element will bind it to this.els.<name>. Adding the same data-name to multiple elements will change this.els.<name> to an Array<HTMLElement>, sorted in DOM order.

Actions

Adding data-action=<name> to an element binds it to click (or optionally data-action-type=<touch|mouseover|etc>). Values can be passed through the event by adding data attributes starting with data-action-.

Create a method in the class matching the name given in the data attribute. Method will be passed: (the element, event object, values)

Properties

  • this.el - References the module element.
  • this.els - Object containing all data-name elements
  • this.options - Object containing anything passed in after data-module- (similar to action values).

constructor

A constructor method can be used but you will need to call super(el). Constructor method gets el as it's only (and required) parameter. super(el) should be called before your code unless you need to modify core behavior. Element binding happens only when super is called.

Required options

A module can pass an array of required options to the super() method. Module will fail to init if any number of the required options are not present. Example: super(el, ['someOption', 'anotherOption'])


A First + Third Project

changelog

5.1.1 / 2017-02-16

  • fixing actionRouter to use currentTarget instead of target

5.1.0 / 2017-02-09

  • adding proper parent checking and uuid

5.0.1 / 2017-02-06

  • adding module property

5.0.0 / 2017-02-05

  • using scriptkit
  • enabling debug from localstorage
  • cleaned setupActions
  • require data-action on top level module
  • adding checks for required actions/options/named
  • using domassist for dom interaction
  • added destroy method
  • allowing default options to be set
  • updated deps

4.0.1 / 2017-01-26

  • updated attrobj
  • removed for of statements
  • removed core-js depedency

4.0.0 / 2016-12-28

  • swap out serializeAttributes with attrObj
  • updated dev deps
  • added travis
  • chore(package): update dependencies
  • removed dist file (will be published with npm)
  • pull in name of module from constructor.name
  • added pre/post init hooks
  • auto discover
  • default debug to false
  • debug mode
  • returns instances from discover call
  • migrated tests, now runs in console

3.1.1 / 2016-12-11

  • Add browserify transform options.

3.1.0 / 2016-12-01

  • Fixes a bug from last version where parentModule wouldn't return correctly.

3.0.0 / 2016-11-24

  • Allows modules to be nested. Potentially breaking change. Check if your modules are nested before upgrading.

2.2.2 / 2016-11-02

  • updated package, removed transform

2.2.1 / 2016-11-02

  • added browser field

2.2.0 / 2016-10-19

  • removed dist/index
  • updated package.json to include transform
  • eslint
  • switched to npm scripts over gulp. moved example to test
  • static method for nodeListToArray
  • added second param to find to turn nodelist into array

2.1.0 / 2016-09-08

  • actions can now be bound to module element

2.0.1 / 2016-07-20

  • use forEach instead of Array.values

2.0.0 / 2016-07-14

  • updated gitignore
  • removed example dist files
  • cleaned up gulp to just have test run through browserify/babel
  • example: test now handles loading files
  • have test import domodule and example
  • example: import domodule and register itself
  • fixed default export
  • BREAKING: added Domodule.register(name, cls);
  • make npm start work
  • export Domodule class