Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So, I decided to throw my own solution in the pot. Here is a <a href="http://jsfiddle.net/w4CHQ/" rel="noreferrer"><strong>working fiddle</strong></a>. Note this only runs on very modern browsers.</p> <h3>What it uses</h3> <p>This implementation is very modern - it requires a (very) modern browser and users two new technologies:</p> <ul> <li><a href="https://developer.mozilla.org/en/docs/Web/API/MutationObserver" rel="noreferrer"><code>MutationObserver</code>s</a> to detect changes in the dom (event listeners are used as well)</li> <li><a href="http://addyosmani.com/blog/the-future-of-data-binding-is-object-observe/" rel="noreferrer"><code>Object.observe</code></a> to detect changes in the object and notifying the dom. <em>Danger, since this answer has been written O.o has been discussed and decided against by the ECMAScript TC, consider a polyfill</em>.</li> </ul> <h3>How it works</h3> <ul> <li>On the element, put a <code>domAttribute:objAttribute</code> mapping - for example <code>bind='textContent:name'</code></li> <li>Read that in the dataBind function. Observe changes to both the element and the object. </li> <li>When a change occurs - update the relevant element.</li> </ul> <h3>The solution</h3> <p>Here is the <code>dataBind</code> function, note it's just 20 lines of code and could be shorter:</p> <pre><code>function dataBind(domElement, obj) { var bind = domElement.getAttribute("bind").split(":"); var domAttr = bind[0].trim(); // the attribute on the DOM element var itemAttr = bind[1].trim(); // the attribute the object // when the object changes - update the DOM Object.observe(obj, function (change) { domElement[domAttr] = obj[itemAttr]; }); // when the dom changes - update the object new MutationObserver(updateObj).observe(domElement, { attributes: true, childList: true, characterData: true }); domElement.addEventListener("keyup", updateObj); domElement.addEventListener("click",updateObj); function updateObj(){ obj[itemAttr] = domElement[domAttr]; } // start the cycle by taking the attribute from the object and updating it. domElement[domAttr] = obj[itemAttr]; } </code></pre> <p>Here is some usage:</p> <p>HTML: </p> <pre><code>&lt;div id='projection' bind='textContent:name'&gt;&lt;/div&gt; &lt;input type='text' id='textView' bind='value:name' /&gt; </code></pre> <p>JavaScript: </p> <pre><code>var obj = { name: "Benjamin" }; var el = document.getElementById("textView"); dataBind(el, obj); var field = document.getElementById("projection"); dataBind(field,obj); </code></pre> <p>Here is a <a href="http://jsfiddle.net/w4CHQ/" rel="noreferrer"><strong>working fiddle</strong></a>. Note that this solution is pretty generic. Object.observe and mutation observer shimming is available.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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