6

Stopping short of full blown frameworks such as Angular, Knockout etc, could anyone recommend a jQuery plugin to simple data binding?

It's needed for a shopping cart one page app that needs to update certain elements on the page after an ajax completion. Just needs to iterate through fields and update the user interface.

Yes, I know I could write something myself, but I don't want to reinvent the wheel if there is something already out there.

My research has lead me to jquery.bindings - but it's not popular ( only one contributor )

Suggestions?

T J
  • 42,762
  • 13
  • 83
  • 138
Simon
  • 2,484
  • 6
  • 35
  • 54

2 Answers2

3

Look into rivets.js.

rivets is a Lightweight (3.4kb minified and gzipped) and powerful data binding and templating library.

Rivets.js is completely agnostic about your model / controller layer and works well with existing libraries that employ an event-driven model such as Backbone.js and Stapes.js. It ships with a built-in adapter for subscribing to plain JS objects using ES5 natives, however this can be replaced with a Watch.JS adapter or an Object.observe adapter.

Some of the features you get out-of-the-box with Rivets.js:

  • Bi-directional data binding to and from DOM nodes.
  • Computed properties through dependency mapping.
  • Formatters to allow mutating values through piping.
  • Iteration binding for binding items in an array.
  • Custom event handlers to fit your ideal workflow.
  • Uniform APIs for easily extending any of the core concepts.

Rivets uses DOM-based templating system:

Instead of parsing and compiling template strings into HTML, Rivets.js wires up your models directly to existing parts of DOM that contain binding declarations and control flow instructions directly on the DOM nodes. You just pass in your models when binding to the parent DOM node and Rivets.js takes care of the rest.

In short, for example assume you want to display the data in a product object like:

var productInfo= {
 name: "test",
 price: 1000
}

in following HTML:

<ul id="product">
  <li>Name</li>
  <li>Price</li>
</ul>

Your can bind the data using rivets like:

rivets.bind($('#product'), {
  product: productInfo // product will be the alias name inside HTML template
});

And the corresponding rivets template will be:

<ul id="product">
  <li rv-text="product.name"></li>
  <li v-text="product.price"></li>
</ul>

or more semantically:

<ul id="product">
  <li data-rv-text="product.name"></li>
  <li data-rv-text="product.price"></li>
</ul>

The rivets.bind method accepts the template element, the model data, as well as any options you wish to override from the main rivets object (optional)


Or if you are binding an array of product objects:

rivets.bind($('#product'), {
  product: ProductArray // where productArray is an array of products
});

You can use iteration bindings using rv-each like:

<ul class="products" data-rv-each-product="products">
  <li data-rv-text="product.name"></li>
  <li data-rv-text="product.price"></li>
</ul>

rivets will create n number of lists according to the items in array.

There are many more cool features which you can find in the guide.

T J
  • 42,762
  • 13
  • 83
  • 138
  • 1
    Thank you for taking the time to answer my question. I started on rolling my own solution based on this question here: http://stackoverflow.com/questions/6491463/accessing-nested-javascript-objects-with-string-key but I can see in the very near future the ability to do more things. I will check it out. Thank you. – Simon Oct 11 '14 at 03:56
0

As long as you are consistent with your class names and returned JSON field names you can update the data with the code below (Note: I did not test this code). Hope this helps. I couldn't find any jQuery plugins other than the one you found that does what you are looking for.

$.ajax({
    url: "/GetCart",
    type: "GET",
    dataType: "json",
    success: function (response) {
        var r = jQuery.parseJSON(response);

        $.each(r, function(key,value) {
        if (jQuery.type(value) == "string") {
            $('.'+key).html(value);
        }
        else if (jQuery.type(value) == "array") {
            $.each(value, function(aindex,avalue) {
                $.each(avalue, function(ikey,ivalue) {
                    $('.'+ikey.toString())[aindex].html(ivalue);
                }
            }
        }
    }
    }
});

Lets say that GetCart returns the following JSON object:

{ 'firstname': 'Bob', 'lastname': 'Ross', 'items': [ { 'desc' : 'car', 'quantity': 1, 'price': 15000.00}, { 'desc' : 'tire', 'quantity': 4, 'price': 200.00} ] }

Also assume you have the following HTML

<form>
Firstname: <span class="firstname">&nbsp;</span><br />
Lastname: <span class="lastname">&nbsp;</span><br />
Items:<br />

<table>
<tr><th>Description</th><th>Quantity</th><th>Price</th></tr>
<tr><td class="desc">&nbsp;</td><td class="quantity">&nbsp;</td><td class="price">&nbsp;</td></tr>
<tr><td class="desc">&nbsp;</td><td class="quantity">&nbsp;</td><td class="price">&nbsp;</td></tr>
</table

</form>
  • Out of curiosity, what does the stack snippets demonstrate...? o.0 – T J Oct 10 '14 at 14:01
  • Thank you for taking the time to answer my question, but it's not going to be suitable for the application. – Simon Oct 11 '14 at 03:59
  • This is the first time I used Stackoverflow. I was trying to display code that was formatted. I will play with it some. – Richard Wagner Oct 11 '14 at 13:20