npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

two-way-data-binding

v2.1.4

Published

Minimal two way data binding in vanilla JS

Downloads

5

Readme

Two way data binding

Minimal no-dependencies 1.2kb (756B gzipped) two way data binding in vanilla JS.

Version Build Status CodeQL Analysis semver: semantic-release License

Install

npm i two-way-data-binding

How to use

It works with DOM elements such as:

  • Layout: div, span, p, headings (anything with textContent or innerHTML)
  • Form elements:
    • Input (including checkbox, radio)
    • Textarea
    • Select

Basic usage

<h1 data-bind="name">Default value</h1>
<input type="text" data-model="name"/>
import TwoWayDataBinding from 'two-way-data-binding'

TwoWayDataBinding({
  dataModel: {
    name: 'My Awesome Site'
  },
})

Several elements with the same bind

<p data-bind="description">Default description</p>
<input type="text" data-bind="description" data-model="description"/>
import TwoWayDataBinding from 'two-way-data-binding'

TwoWayDataBinding()

Update model via JavaScript

You can use deep objects too:

const proxy = TwoWayDataBinding({
  dataModel: {
    site: {
      name: 'My Awesome Site'
    }
  },
})

proxy.site.name = `New name`;

Configuration

$context

Optional. Defines the context of the data model. Defaults to document

attributeBind

Optional. Defines the attribute to bind to in your HTML. Defaults to data-bind

attributeModel

Optional. Defines the attribute to bind to in your HTML. Defaults to data-model

$myInput.addEventListener('twowaydatabinding:change', () => {
  // This will be fired after the native change, after we update the state
});

dataModel

Optional. Defines the data model. Defaults to {}

events

Optional. Defines the events to bind to. Defaults to [`keyup`, `change`]

pathDelimiter

Optional. Defines the path delimiter in your data-bind attributes such as header.site.name. Defaults to .

Custom events handling

In order to allow consumers to perform actions after TwoWayDataBinding performs model update, a custom event is fired with the name twowaydatabinding:change|keyup|eventname. By default is dispatched in change and keyup events, but it can be extended by setting more events in the array of the TwoWayDataBinding configuration, meaning a twowaydatabinding:[eventname] will be fired after the logic is executed, and the consumer application can subscribe to them to perform any needed logic after the library has done the operations.

twowaydatabinding:setcustomvalue

This event works the other way round, from the consumer application to TwoWayDataBinding, which is listening to that event and responds by setting in the data model the value passed for a given property.

The library can prevent setting the value to the model for certain custom-value properties (see attributeCustomValue), and for those cases, a custom event twowaydatabinding:setcustomvalue can be fired in order to set a value into the model. A twowaydatabinding:change event is fired again after performing this action.

To set the new value in the data model, dispatch the event in the correspondant element passing a detail object populated with path (property path of the property to change in the data model object) and value properties.

To trigger it from your application:

input.dispatchEvent(new CustomEvent(`twowaydatabinding:setcustomvalue`, {
  bubbles: true,
  cancelable: true,
  detail: {
    path: input.getAttribute(config.attributeModel),
    value: dataValue
  }
}));

proxy.propName // 'newValue'

Setting a custom value to a bound input instead of his value in the data model

attributeCustomValue

Optional. Defines an array of attributes to prevent the model being updated with the input value when change or keyup by default. Defaults to ['data-value'].

If an input has data-model and data-bind attributes at the same time, when this input value is changed, the model gets updated with that input value property. Nevertheless, this can be prevented by adding a custom attribute which will be the host of the actual value.

By default is ['data-value'] (but can be customised and extended), and when an input has one of these attributes, the model is not updated with the value of the input and it should be manually updated by the consumer's application logic by dispatching a custom event twowaydatabinding:setcustomvalue. See Custom Events handling

Browser support

The script does not include any polyfills.

| Browser | Version | |---------------|---| | Google Chrome | >= 96 | | Edge | >= 92 | | Firefox | >= 91 | | Safari | >= 15.4 | | Opera | >= 76 | | IE | ❌ |

How to develop

You might want to develop locally. Spawn a local server with:

npm run develop

You can change /develop/index.html and /develop/index.js to your liking.