Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery data binding library or plugin recommendation [closed]

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?

like image 430
Simon Avatar asked Oct 07 '14 21:10

Simon


People also ask

Is jQuery obsolete?

The team announced that the cross-platform jQuery Mobile project under its umbrella will be fully deprecated as of October 7, 2021. New technologies for mobile app development have evolved since this project was launched in 2010, so we're encouraging developers to plan for this jQuery Mobile transition.

What can we use instead of in jQuery?

A progressive JavaScript framework, Vue. js is considered a good alternative to jQuery. It is an open-source, MVVM, front-end JS framework that is considered ideal to create user interfaces and single-page apps. It is also considered good for web interfaces, desktop, and mobile app development.

What does .data do in jQuery?

version added: 1.4. data() method allows us to attach data of any type to DOM elements in a way that is safe from circular references and therefore from memory leaks. Using the data() method to update data does not affect attributes in the DOM. To set a data-* attribute value, use attr .

How have you stored and referenced data in your jQuery code?

Basically jQuery holds the information you store/retrieve with data(name, value)/data(name) and remove with removeData(name) in an internal javascript object named cache . The rest is just a bit of javascript magic to make it work and keep all the associations right. Oh and to answer the first part of your question.


1 Answers

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.

like image 176
T J Avatar answered Nov 15 '22 05:11

T J