Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Data-binding is traditionally handled best by other libraries, such as Knockoutjs. That being said, there are ways in which you can accomplish similar results in jQuery. One simple one is to merely publish an event when our updated list comes in, and rebuilt our DOM from that.</p> <pre class="lang-js prettyprint-override"><code>(function () { "use strict"; var $list = $("#users .list"); var tmplt = "&lt;li&gt;&lt;h3&gt;{{name}}&lt;/h3&gt;&lt;/li&gt;"; $list.on( "updatedList", updateList ); function updateList ( event, names ) { $list.html($.map( names, function ( name ) { return tmplt.replace( /{{name}}/, name ); }).join("")); } // Anytime you want to update, pass the new names into a trigger $list.trigger( "updatedList", [ [ "Bob", "Richard", "Kendra", "Jake" ] ] ); }()); </code></pre> <p>Demo: <a href="http://jsfiddle.net/wDFKC/1/" rel="nofollow">http://jsfiddle.net/wDFKC/1/</a></p> <p>Please note that while you can do this with jQuery, it is best handled by other tools like Knockoutjs. The reason being is that other tools are a bit smarter, and will make decisions about which parts of the DOM need to be changed.</p> <p>For instance, if a user is logged in according to our present list, and our updated list, there is no reason why we should be removing their entry, only to add it again. You can use jQuery along side Knockoutjs, so the two work nicely together in an app. You should take a few minutes and read about the benefits of using <a href="http://knockoutjs.com/documentation/observableArrays.html" rel="nofollow">Observable Arrays</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload