data-binding

Data attribute binding and interpolation

binding

Data binding for modern browsers...and IE8 >

binding is an automatic way of updating your HTML whenever the underlying data changes. It does one thing and does it right! It works well with your favorite framework and has been built to suit your development needs.

binding is highly extensible, like jQuery you can create your own plugins or reuse some others. The possibilities are limitless and the barrier is so low that you'll want to do it straight away.

Here's a list of available plugins:

  • list create a list by instantiating a template once per item from a collection
  • event listen or delegate events with automatic touch support
  • control toggle or radio elements has never been so easy
  • stack create a stack of DOM elements for tab-based navigation
  • bind attribute double way binding
  • hidden hide you dom element when the data changes
  • html bind inner html to data
  • text bind inner text to data
$ component install bredele/binding

standalone:

<script src="binding.js"></script>

An example is worth a thousand words.

<span id="card">My github is {github}</span>

By default, binding applies variable substitution ({variable}) from the data model.

var binding = require('binding'),
    el = document.getElementById('card');
 
binding({
  github: 'bredele'
}).apply(el);

You can use variable substitution in every possible HTML and SVG attribute. Here's an other example:

<span id="card" class="{github}">
  My github is <a href="http://github.com/{github}">{github}</a>
</span>

The result is:

<span id="card" class="bredele">
  My github is <a href="http://github.com/bredele">bredele</a>
</span>

A binding plugin is simply a function or an object that we use to extend the HTML capabilities. It becomes more expressive, configurable and dynamic.

binding()
  .add('list', list)
  .apply(el);

You choose what plugin you want to use: your application does just what you need and nothing more.

<ul list>
  <li></li>
</ul>

By giving a name to your plugins, you avoid scope conflicts and you can reuse a plugin multiple times.

Writing a plugin is as simple as writing a JavaScript function.

binding({
    github: 'bredele'
  })
  .add('data-nickname', function(nodestr) {
    node.innerHTML = "I am " + this[str];
  })
  .apply(el);

A function plugin always has the dom element as first argument and its scope (this) is the data model you passed in the constructor.

<span data-nickname="github"></span>

In the example above, we define a plugin called data-nickname that set the content of the dom element with the value of the attribute data-nickname. The result is:

<span data-nickname="github">I am bredele</span>

We advise you to use store as data model. Because store is based on an emitter, you'll be able to listen and react when the data changes. See bind for a simple example using store events.

We used to say one function (or unit) equal one functionality. Potentially, a plugin can be more complex than just a function. That's the reason why a plugin can also be a JavaScript object. An object will give you more structure and control over your plugin.

var state = {
  type: 'blog',
  text: 'binding component is awesome'
};
 
binding(state)
  .add('data-state', {
    addfunction(nodeattrdata) {
      if(state.type === 'blog') node[attr] = state[data];
    },
    hidefunction(node) {
     if(state.type === 'blog') el.className = 'hidden';
    }
  })
  .apply(el);

From the HTML markup, you can call a plugin method (here add and remove) with the following syntax method:arg,arg. You'll find the entire spec at this link.

<section>
  <article data-state="add:innerHTML,text"></article>
  <div data-state="hide"></div>
</section>

A plugin can be the backbone of your application, making easy to invent new HTML features and create reusable components.

See list for a great example of object binding. List can be used as a plugin or as an isolated component. That's one of the benefit to have objects as binding.

binding can be used with SVG elements and makes easy to create dynamic and real time charts. However, SVG doesn't support custom attributes and you should prefix your plugins with data- (dataset attributes).

binding is not invasive and free from boilerplate: it lets you write the old plain JavaScript you like. It has been built though with some concepts such as inversion of controls and configuration because it allows you to control what is happening in your application, to easily test, maintain or reuse your plugins. Please keep that in mind when you'll write your own plugins.

binding is a work in progress. We will probably make the object plugin spec better.

initialize a binding with a model object.

var binding = require('binding');
binding({});

or

var Binding = require('binding');
 
var binding = new Binding({});

add attribute bindings (functions or objects) by name

binding({}).add('bredele', plugin);  

apply bindings on a given dom node

binding({})
  .add('bredele', plugin)
  .apply(document.body);  

The MIT License (MIT)

Copyright (c) 2014 Olivier Wietrich olivier.wietrich@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.