Code Monkey home page Code Monkey logo

bindi's Introduction

bindi

binding for custom elements.

demo

This is an experimental library that provides data binding support for vanilla v1 custom elements

This means that you can write your custom elements as close to the standard, bindi just saves you some boilerplating.

At this time the lib is just an experiment. This is not for use in a real project.

why?

Because the custom element v1 spec is pretty good and the only thing that was preventing me from using without any frameworks was the lack of data binding.

So I thought 'if i could allow a way for data-binding to be defined, then convert that to logic belonging to the custom element that'd be great'.

custom-elements and databinding ... why not polymer?

Because polymer makes heavy use of html imports, which is unlikely to become a standard.

how?

bindi exposes a method prepare that takes markup like:

<div>[[name]]</div>

and returns an object: {markup:string, bind: (el:HTMLElement) => void}. the markup looks like:

<div><span bindi-id="0"></span></div>

you then call bind with your custom element: bind(this), this will add a setter for name to your custom element, such that whenever it's set it'll update [bindi-id="0"] with the value.

a simple example

import {prepare} from 'bindi';

const binding = prepare(`<div>[[name]]</div>`);

class MyEl extends HTMLElement{
  constructor(){
    super();
    const sr = this.attachShadow({mode: 'open'});
    sr.innerHTML = binding.markup;
    binding.bind(this);
  }
}
customElements.define('my-el', MyEl);

document.querySelector('my-el').name = 'Ed'; //my-el will now contain 'Ed'.

The binding definitions are trying to follow polymer syntax as much as possible.

install

npm install

demo

The demo shows a few elements working together with changes travelling up and down the dom hierarchy.

cd demo
../node_modules/.bin/webpack-dev-server --hot --inline
# go to http://localhost:8080

supported bindings

  • single prop expression: {{foo}}
  • custom event names: x="{{foo::input}}"
  • nested prop expression: {{foo.bar}}
  • on-click="xxxxx" - just to get an idea of how to handle events.

not supported

  • computed expressions? no
  • attribute bindings? no

kind of supported?

  • arrays? see dom-repeat in demo as a rough guess on how to achieve this.

notes

Uses polymer convention of adding an event listener for a binding so: <el foo="{{bar}}"> adds:

el.addEventListener('foo-changed', () => {
  this.bar = el.foo;
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.