diff --git a/ui/wolf.ui.js b/ui/wolf.ui.js index bb87092..fb68514 100644 --- a/ui/wolf.ui.js +++ b/ui/wolf.ui.js @@ -323,7 +323,7 @@ case null: break; case "attr": { - if (c.$.length != 1 || c.$[0].$t) + if (!c.$ || c.$.length != 1 || c.$[0].$t) throw new Error("Control definition wolf:" + id + " attribute name missing or type error"); var name = c.$[0].$; if (name[0] == "$" || name == "ui") @@ -339,7 +339,7 @@ break; } case "event": { - if (c.$.length != 1 || c.$[0].$t) + if (!c.$ || c.$.length != 1 || c.$[0].$t) throw new Error("Control definition wolf:" + id + " event name missing or type error"); var name = c.$[0].$; if (controller[name]) @@ -419,8 +419,6 @@ throw new Error("wolf:" + id + " does not have ui defined"); } - script.init && script.init(); - // Event mirror and attribute hook for (var k in values) if (k.startsWith("event:")) { @@ -470,6 +468,7 @@ } } //TODO: Create control defined custom properties for attributes that change the attribute values + script.init && script.init(); // Generate DOM return renderControlDOM(ext); diff --git a/wolf.js b/wolf.js index 2a78edf..80012a2 100644 --- a/wolf.js +++ b/wolf.js @@ -748,6 +748,13 @@ var UI = (() => { var frgs = {}, navigatorController; + /** + * Template class + */ + function Template() { + + } + /** Initializes an application, an application is only a definition of the container element in the document * and the url of the application controller, a single web page can contains several applications * @param {string} ctrlUrl url for the application controller @@ -859,8 +866,7 @@ */ "repeat": { bindable: true, - initTemplate: template => { - var items = template.repeat; + initTemplate: (template, items) => { if (!(items instanceof D.Binding)) throw new Error("Only bindings can be used on xwolf:repeat"); delete template.repeat; @@ -881,6 +887,17 @@ processor: function (element, value, template) { element.setContextPath(value); } + }, + + /** + * wolf:id + * for tagging templates + */ + "id": { + bindable: false, + initTemplate: (template, value) => { + template.$id = value; + } } } })(); @@ -1185,7 +1202,7 @@ * @param {*} template template processed completely */ - var templ = {}, initiators = []; + var templ = new Template(), initiators = []; function valOrBinding(val) { if (typeof val === "string" && val.indexOf('{') >= 0) @@ -1257,11 +1274,13 @@ throw new Error("Unknown global attribute wolf:" + attr); if (aval instanceof D.Binding && !wattr.bindable) throw new Error(attr + " does not allow binding"); - (value => templ[attrName] = element => wattr.processor(element, value, templ))(aval); + if (wattr.processor) + templ[attrName] = element => wattr.processor(element, aval, templ); //TODO: do not like this solution, maily for wolf:repeat attribute because element // get relocated then repeating tr or tds inside a table. // Better try to parse the HTML manually when reading templates to avoid node relocations - wattr.initTemplate && initiators.push(wattr.initTemplate); + if (wattr.initTemplate) + initiators.push(ttempl => wattr.initTemplate(ttempl, aval)); } else if (attr && attr.substr(0, 5) == "bind:") { attr = attr.substr(5); if (attr[0] == "$") @@ -1696,7 +1715,7 @@ // Does not clone events?? delete clone.$e; - if (templ.$ && Array.isArray(templ.$)) + if (templ.$ && Array.isArray(templ.$)) clone.$ = cloneTemplate(templ.$); return clone; } @@ -1720,6 +1739,8 @@ // function hackTemplate(templ, data) { // } + //Template.prototype + return { initApp: initApp, instanceTemplate: instanceTemplate,