r715 - in funjs/public_html: . funjs
Author: jruchaud Date: 2014-06-20 16:04:06 +0200 (Fri, 20 Jun 2014) New Revision: 715 Url: http://forge.nuiton.org/projects/sandbox/repository/revisions/715 Log: First loop implemenation Modified: funjs/public_html/funjs/TemplateEngine.js funjs/public_html/index.html Modified: funjs/public_html/funjs/TemplateEngine.js =================================================================== --- funjs/public_html/funjs/TemplateEngine.js 2014-06-19 11:09:11 UTC (rev 714) +++ funjs/public_html/funjs/TemplateEngine.js 2014-06-20 14:04:06 UTC (rev 715) @@ -33,17 +33,18 @@ for (var i = 0; i < l; i++) { var child = children[i]; - + if (child instanceof HTMLElement) { this.parse(child, data); - } else { + } else if (child) { this.parseText(child, data); } } }, parseElement: function(element, data) { + var self = this; var datas = element.dataset; if (datas) { @@ -57,6 +58,44 @@ if (datas.click && this.currentCtrl) { element.onclick = this.currentCtrl[datas.click]; } + + if (datas.loop) { + // Options + var loopVar = datas.loopVar || "item"; + var loopIndex = datas.loopIndex || "index"; + + var fragment = document.createDocumentFragment(); + var wrapper = document.createElement("div"); + fragment.appendChild(wrapper); + + var loop = this.parseValue(datas.loop, data) || []; + var copy = element.outerHTML; + + var index = 0; + loop.forEach(function(item) { + wrapper.innerHTML = copy; + + var child = wrapper.children[0]; + delete child.dataset.loop; + delete child.dataset.loopVar; + delete child.dataset.loopIndex; + fragment.appendChild(child); + + data[loopVar] = item; + data[loopIndex] = index ++; + self.parse(child, data); + }); + fragment.removeChild(wrapper); + + delete data[loopVar]; + delete data[loopIndex]; + + element.parentNode.replaceChild(fragment, element); + } + + if (datas.if) { + + } } }, Modified: funjs/public_html/index.html =================================================================== --- funjs/public_html/index.html 2014-06-19 11:09:11 UTC (rev 714) +++ funjs/public_html/index.html 2014-06-20 14:04:06 UTC (rev 715) @@ -17,6 +17,9 @@ <div id="hello"> <p>Hello World {{from}} to {{to?}}!</p> <p>{{test ? value : other}}</p> + <ul> + <li data-loop="list" data-loop-var="v" data-loop-index="i">{{i}} {{v}}</li> + </ul> </div> </script> @@ -30,7 +33,8 @@ value : "YES", other : "NO", from : "me", - to : "you" + to : "you", + list : ["tutu", "titi", "tata", "toto"] }); },
participants (1)
-
jruchaud@users.nuiton.org