Bindy.js

Simple, declarative, drop-in solution for basic data-binding.

For people who like jQuery and HTML.

What bindy does

  • Bind data from html inputs to JS objects
  • Bind data to text nodes from JS objects
  • Filter data before it binds to the DOM
  • Use hooks to transform data before it gets bound to JS

Quick Start

Here's how bindy does data-binding


  <input type="text" data-bind-to="person.name">
  <input type="text" data-bind-to="person.job">
  <input type="text" data-bind-to="person.age">
  <p data-bind-from="person.name"></p>
  <p data-bind-from="person.job"></p>
  <p data-bind-from="person.age"></p>

To get this to work you just need to add the following initialization to your HTML.


  <script src="jquery.js"></script>
  <script src="jquery.bindy.js"></script>
  <script>
    // initialize bindy!
    $.bindy();
  </script>

Other Fun Stuff

Live JS object

The Bindy function returns the data object that stays up-to-date, so you can keep it in a variable and use the updated values when you need to.


  <input type="text" data-bind-to="person.name">
  <input type="text" data-bind-to="person.job">
  <input type="text" data-bind-to="person.age">

    var userInputs = $.bindy();
    console.log(userInputs);
     /* {
        person: {
          name: "...",
          job: "...",
          age: "...",
        }
     */ }

Scopes

The above "quick start" example scopes the data binding at the Document level. If you want data-binding in, say, your sidebar section and footer section, you can scope multiple instances of bindy to DOM nodes.


  // data-binding only happens within the sidebar
  $('#my-sidebar').bindy();


  // data-binding only happens within the footer
  $('#my-footer').bindy();

Filters

Filters are quick helpers for formatting data before it gets rendered. You can register filters with bindy in the configuration object that gets passed on initialization.


  <input type="text" data-bind-to="job.salary">
  <p data-bind-from="job.salary | toCurrency"></p>
  

  $.bindy({
    filters: {
      toCurrency: function(string) {
        return '$ '+(parseInt(string, 10) || 0).toFixed(2);
      }
    }
  });

BeforeUpdate Hook

Configure Bindy with a beforeUpdate function to dive in and manipulate the data object before it gets saved as a JS object.


  <input type="text" data-bind-to="person.firstName">
  <input type="text" data-bind-to="person.lastName">
  <p data-bind-from="person.fullName"></p>

  $.bindy({
    beforeUpdate: function(model) {
      model.fullName = model.firstName+' '+model.lastName;
      return model;
    }
  });