Include the root element when building the bindings for a view.
Showing
3 changed files
with
30 additions
and
23 deletions
... | @@ -107,35 +107,32 @@ | ... | @@ -107,35 +107,32 @@ |
107 | }; | 107 | }; |
108 | 108 | ||
109 | View.prototype.build = function() { | 109 | View.prototype.build = function() { |
110 | var attribute, bindType, bindingRegExp, eventRegExp, keypath, model, node, path, pipe, pipes, type, _i, _len, _ref, _results; | 110 | var bindingRegExp, eventRegExp, node, parseNode, _i, _len, _ref, _results, |
111 | _this = this; | ||
111 | this.bindings = []; | 112 | this.bindings = []; |
112 | bindingRegExp = this.bindingRegExp(); | 113 | bindingRegExp = this.bindingRegExp(); |
113 | eventRegExp = /^on-/; | 114 | eventRegExp = /^on-/; |
114 | _ref = this.el.getElementsByTagName('*'); | 115 | parseNode = function(node) { |
116 | var attribute, bindType, keypath, model, path, pipe, pipes, type, _i, _len, _ref, _results; | ||
117 | _ref = node.attributes; | ||
115 | _results = []; | 118 | _results = []; |
116 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 119 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
117 | node = _ref[_i]; | 120 | attribute = _ref[_i]; |
118 | _results.push((function() { | ||
119 | var _j, _len1, _ref1, _results1; | ||
120 | _ref1 = node.attributes; | ||
121 | _results1 = []; | ||
122 | for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { | ||
123 | attribute = _ref1[_j]; | ||
124 | if (bindingRegExp.test(attribute.name)) { | 121 | if (bindingRegExp.test(attribute.name)) { |
125 | bindType = "attribute"; | 122 | bindType = "attribute"; |
126 | type = attribute.name.replace(bindingRegExp, ''); | 123 | type = attribute.name.replace(bindingRegExp, ''); |
127 | pipes = (function() { | 124 | pipes = (function() { |
128 | var _k, _len2, _ref2, _results2; | 125 | var _j, _len1, _ref1, _results1; |
129 | _ref2 = attribute.value.split('|'); | 126 | _ref1 = attribute.value.split('|'); |
130 | _results2 = []; | 127 | _results1 = []; |
131 | for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) { | 128 | for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { |
132 | pipe = _ref2[_k]; | 129 | pipe = _ref1[_j]; |
133 | _results2.push(pipe.trim()); | 130 | _results1.push(pipe.trim()); |
134 | } | 131 | } |
135 | return _results2; | 132 | return _results1; |
136 | })(); | 133 | })(); |
137 | path = pipes.shift().split('.'); | 134 | path = pipes.shift().split('.'); |
138 | model = this.models[path.shift()]; | 135 | model = _this.models[path.shift()]; |
139 | keypath = path.join('.'); | 136 | keypath = path.join('.'); |
140 | if (eventRegExp.test(type)) { | 137 | if (eventRegExp.test(type)) { |
141 | type = type.replace(eventRegExp, ''); | 138 | type = type.replace(eventRegExp, ''); |
... | @@ -143,13 +140,19 @@ | ... | @@ -143,13 +140,19 @@ |
143 | } else if (__indexOf.call(bidirectionals, type) >= 0) { | 140 | } else if (__indexOf.call(bidirectionals, type) >= 0) { |
144 | bindType = "bidirectional"; | 141 | bindType = "bidirectional"; |
145 | } | 142 | } |
146 | _results1.push(this.bindings.push(new Rivets.Binding(node, type, bindType, model, keypath, pipes))); | 143 | _results.push(_this.bindings.push(new Rivets.Binding(node, type, bindType, model, keypath, pipes))); |
147 | } else { | 144 | } else { |
148 | _results1.push(void 0); | 145 | _results.push(void 0); |
149 | } | 146 | } |
150 | } | 147 | } |
151 | return _results1; | 148 | return _results; |
152 | }).call(this)); | 149 | }; |
150 | parseNode(this.el); | ||
151 | _ref = this.el.getElementsByTagName('*'); | ||
152 | _results = []; | ||
153 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
154 | node = _ref[_i]; | ||
155 | _results.push(parseNode(node)); | ||
153 | } | 156 | } |
154 | return _results; | 157 | return _results; |
155 | }; | 158 | }; | ... | ... |
1 | (function(){var a,b,c,d,e,f,g,h,i=function(a,b){return function(){return a.apply(b,arguments)}},j=[].indexOf||function(a){for(var b=0,c=this.length;b<c;b++)if(b in this&&this[b]===a)return b;return-1};a={},String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),a.Binding=function(){function c(c,d,f,g,h,j){this.el=c,this.type=d,this.bindType=f,this.model=g,this.keypath=h,this.formatters=j!=null?j:[],this.unbind=i(this.unbind,this),this.publish=i(this.publish,this),this.bind=i(this.bind,this),this.set=i(this.set,this),this.bindType==="event"?this.routine=e(this.type):this.routine=a.routines[this.type]||b(this.type)}return c.prototype.set=function(b){var c,d,e,f;f=this.formatters;for(d=0,e=f.length;d<e;d++)c=f[d],b=a.config.formatters[c](b);return this.bindType==="event"?(this.routine(this.el,b,this.currentListener),this.currentListener=b):this.routine(this.el,b)},c.prototype.bind=function(){a.config.adapter.subscribe(this.model,this.keypath,this.set),a.config.preloadData&&this.set(a.config.adapter.read(this.model,this.keypath));if(this.bindType==="bidirectional")return d(this.el,"change",this.publish)},c.prototype.publish=function(b){var c;return c=b.target||b.srcElement,a.config.adapter.publish(this.model,this.keypath,f(c))},c.prototype.unbind=function(){a.config.adapter.unsubscribe(this.model,this.keypath,this.set);if(this.bindType==="bidirectional")return this.el.removeEventListener("change",this.publish)},c}(),a.View=function(){function b(a,b){this.el=a,this.models=b,this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.build=i(this.build,this),this.bindingRegExp=i(this.bindingRegExp,this),this.el.jquery&&(this.el=this.el.get(0)),this.build()}return b.prototype.bindingRegExp=function(){var b;return b=a.config.prefix,b?new RegExp("^data-"+b+"-"):/^data-/},b.prototype.build=function(){var b,d,e,f,g,h,i,k,l,m,n,o,p,q,r;this.bindings=[],e=this.bindingRegExp(),f=/^on-/,q=this.el.getElementsByTagName("*"),r=[];for(o=0,p=q.length;o<p;o++)i=q[o],r.push(function(){var o,p,q,r;q=i.attributes,r=[];for(o=0,p=q.length;o<p;o++)b=q[o],e.test(b.name)?(d="attribute",n=b.name.replace(e,""),m=function(){var a,c,d,e;d=b.value.split("|"),e=[];for(a=0,c=d.length;a<c;a++)l=d[a],e.push(l.trim());return e}(),k=m.shift().split("."),h=this.models[k.shift()],g=k.join("."),f.test(n)?(n=n.replace(f,""),d="event"):j.call(c,n)>=0&&(d="bidirectional"),r.push(this.bindings.push(new a.Binding(i,n,d,h,g,m)))):r.push(void 0);return r}.call(this));return r},b.prototype.bind=function(){var a,b,c,d,e;d=this.bindings,e=[];for(b=0,c=d.length;b<c;b++)a=d[b],e.push(a.bind());return e},b.prototype.unbind=function(){var a,b,c,d,e;d=this.bindings,e=[];for(b=0,c=d.length;b<c;b++)a=d[b],e.push(a.unbind());return e},b}(),d=function(a,b,c){return window.addEventListener?a.addEventListener(b,c):a.attachEvent(b,c)},h=function(a,b,c){return window.removeEventListener?a.removeEventListener(b,c):a.detachEvent(b,c)},f=function(a){switch(a.type){case"text":case"textarea":case"password":case"select-one":return a.value;case"checkbox":case"radio":return a.checked}},e=function(a){return function(b,c,e){c&&d(b,a,c);if(e)return h(b,a,e)}},b=function(a){return function(b,c){return c?b.setAttribute(a,c):b.removeAttribute(a)}},c=["value","checked","unchecked","selected","unselected"],a.routines={enabled:function(a,b){return a.disabled=!b},disabled:function(a,b){return a.disabled=!!b},checked:function(a,b){return a.checked=!!b},unchecked:function(a,b){return a.checked=!b},selected:function(a,b){return a.selected=!!b},unselected:function(a,b){return a.selected=!b},show:function(a,b){return a.style.display=b?"":"none"},hide:function(a,b){return a.style.display=b?"none":""},html:function(a,b){return a.innerHTML=b||""},value:function(a,b){return a.value=b},text:function(a,b){return a.innerText!=null?a.innerText=b||"":a.textContent=b||""}},a.config={preloadData:!0},g={configure:function(b){var c,d,e;b==null&&(b={}),e=[];for(c in b)d=b[c],e.push(a.config[c]=d);return e},register:function(b,c){return a.routines[b]=c},bind:function(b,c){var d;return c==null&&(c={}),d=new a.View(b,c),d.bind(),d}},typeof module!="undefined"&&module!==null?module.exports=g:this.rivets=g}).call(this); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
1 | (function(){var a,b,c,d,e,f,g,h,i=function(a,b){return function(){return a.apply(b,arguments)}},j=[].indexOf||function(a){for(var b=0,c=this.length;b<c;b++)if(b in this&&this[b]===a)return b;return-1};a={},String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),a.Binding=function(){function c(c,d,f,g,h,j){this.el=c,this.type=d,this.bindType=f,this.model=g,this.keypath=h,this.formatters=j!=null?j:[],this.unbind=i(this.unbind,this),this.publish=i(this.publish,this),this.bind=i(this.bind,this),this.set=i(this.set,this),this.bindType==="event"?this.routine=e(this.type):this.routine=a.routines[this.type]||b(this.type)}return c.prototype.set=function(b){var c,d,e,f;f=this.formatters;for(d=0,e=f.length;d<e;d++)c=f[d],b=a.config.formatters[c](b);return this.bindType==="event"?(this.routine(this.el,b,this.currentListener),this.currentListener=b):this.routine(this.el,b)},c.prototype.bind=function(){a.config.adapter.subscribe(this.model,this.keypath,this.set),a.config.preloadData&&this.set(a.config.adapter.read(this.model,this.keypath));if(this.bindType==="bidirectional")return d(this.el,"change",this.publish)},c.prototype.publish=function(b){var c;return c=b.target||b.srcElement,a.config.adapter.publish(this.model,this.keypath,f(c))},c.prototype.unbind=function(){a.config.adapter.unsubscribe(this.model,this.keypath,this.set);if(this.bindType==="bidirectional")return this.el.removeEventListener("change",this.publish)},c}(),a.View=function(){function b(a,b){this.el=a,this.models=b,this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.build=i(this.build,this),this.bindingRegExp=i(this.bindingRegExp,this),this.el.jquery&&(this.el=this.el.get(0)),this.build()}return b.prototype.bindingRegExp=function(){var b;return b=a.config.prefix,b?new RegExp("^data-"+b+"-"):/^data-/},b.prototype.build=function(){var b,d,e,f,g,h,i,k,l=this;this.bindings=[],b=this.bindingRegExp(),d=/^on-/,f=function(e){var f,g,h,i,k,m,n,o,p,q,r,s;r=e.attributes,s=[];for(p=0,q=r.length;p<q;p++)f=r[p],b.test(f.name)?(g="attribute",o=f.name.replace(b,""),n=function(){var a,b,c,d;c=f.value.split("|"),d=[];for(a=0,b=c.length;a<b;a++)m=c[a],d.push(m.trim());return d}(),k=n.shift().split("."),i=l.models[k.shift()],h=k.join("."),d.test(o)?(o=o.replace(d,""),g="event"):j.call(c,o)>=0&&(g="bidirectional"),s.push(l.bindings.push(new a.Binding(e,o,g,i,h,n)))):s.push(void 0);return s},f(this.el),i=this.el.getElementsByTagName("*"),k=[];for(g=0,h=i.length;g<h;g++)e=i[g],k.push(f(e));return k},b.prototype.bind=function(){var a,b,c,d,e;d=this.bindings,e=[];for(b=0,c=d.length;b<c;b++)a=d[b],e.push(a.bind());return e},b.prototype.unbind=function(){var a,b,c,d,e;d=this.bindings,e=[];for(b=0,c=d.length;b<c;b++)a=d[b],e.push(a.unbind());return e},b}(),d=function(a,b,c){return window.addEventListener?a.addEventListener(b,c):a.attachEvent(b,c)},h=function(a,b,c){return window.removeEventListener?a.removeEventListener(b,c):a.detachEvent(b,c)},f=function(a){switch(a.type){case"text":case"textarea":case"password":case"select-one":return a.value;case"checkbox":case"radio":return a.checked}},e=function(a){return function(b,c,e){c&&d(b,a,c);if(e)return h(b,a,e)}},b=function(a){return function(b,c){return c?b.setAttribute(a,c):b.removeAttribute(a)}},c=["value","checked","unchecked","selected","unselected"],a.routines={enabled:function(a,b){return a.disabled=!b},disabled:function(a,b){return a.disabled=!!b},checked:function(a,b){return a.checked=!!b},unchecked:function(a,b){return a.checked=!b},selected:function(a,b){return a.selected=!!b},unselected:function(a,b){return a.selected=!b},show:function(a,b){return a.style.display=b?"":"none"},hide:function(a,b){return a.style.display=b?"none":""},html:function(a,b){return a.innerHTML=b||""},value:function(a,b){return a.value=b},text:function(a,b){return a.innerText!=null?a.innerText=b||"":a.textContent=b||""}},a.config={preloadData:!0},g={configure:function(b){var c,d,e;b==null&&(b={}),e=[];for(c in b)d=b[c],e.push(a.config[c]=d);return e},register:function(b,c){return a.routines[b]=c},bind:function(b,c){var d;return c==null&&(c={}),d=new a.View(b,c),d.bind(),d}},typeof module!="undefined"&&module!==null?module.exports=g:this.rivets=g}).call(this); | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -74,7 +74,8 @@ class Rivets.View | ... | @@ -74,7 +74,8 @@ class Rivets.View |
74 | @bindings = [] | 74 | @bindings = [] |
75 | bindingRegExp = @bindingRegExp() | 75 | bindingRegExp = @bindingRegExp() |
76 | eventRegExp = /^on-/ | 76 | eventRegExp = /^on-/ |
77 | for node in @el.getElementsByTagName '*' | 77 | |
78 | parseNode = (node) => | ||
78 | for attribute in node.attributes | 79 | for attribute in node.attributes |
79 | if bindingRegExp.test attribute.name | 80 | if bindingRegExp.test attribute.name |
80 | bindType = "attribute" | 81 | bindType = "attribute" |
... | @@ -92,6 +93,9 @@ class Rivets.View | ... | @@ -92,6 +93,9 @@ class Rivets.View |
92 | 93 | ||
93 | @bindings.push new Rivets.Binding node, type, bindType, model, keypath, pipes | 94 | @bindings.push new Rivets.Binding node, type, bindType, model, keypath, pipes |
94 | 95 | ||
96 | parseNode @el | ||
97 | parseNode node for node in @el.getElementsByTagName '*' | ||
98 | |||
95 | # Binds all of the current bindings for this view. | 99 | # Binds all of the current bindings for this view. |
96 | bind: => | 100 | bind: => |
97 | binding.bind() for binding in @bindings | 101 | binding.bind() for binding in @bindings | ... | ... |
-
Please register or sign in to post a comment