Arthur Brandao 5 жил өмнө
parent
commit
bde3542aee
2 өөрчлөгдсөн 14 нэмэгдсэн , 0 устгасан
  1. 5 0
      src/index.html
  2. 9 0
      src/js/binder.js

+ 5 - 0
src/index.html

@@ -18,6 +18,7 @@
     <h3 data-bind="test"></h3>
 
     <div data-loop="aze">
+      <h4>Loop</h4>
       <span data-val="a"></span>
     </div>
 
@@ -41,6 +42,10 @@
       });
 
       looper(scope);
+
+      scope.aze[1].a = 'lol';
+
+      looper(scope);
     </script>
   </body>
 </html>

+ 9 - 0
src/js/binder.js

@@ -80,6 +80,15 @@ function looper(scope) {
     let html = [];
     // If data is an array (if not do nothing)
     if (data && Array.isArray(data)) {
+      // If data is already computed back to the initial value
+      const defaultValue = el.querySelector('[data-looped]')
+      if (defaultValue)  {
+        el.innerHTML = defaultValue.innerHTML;
+      }
+
+      // Add the not computed value
+      html.push(`<div data-looped="${propName}" style="display: none">${el.innerHTML}</div>`);
+
       // Add info to replace value
       for (let i = 0; i < data.length; i++) {
         html.push(el.innerHTML.replace(/data-val="/g, 'data-index="' + i + '" data-val="'));