Build 0.3.3.
Showing
4 changed files
with
25 additions
and
7 deletions
1 | // rivets.js | 1 | // rivets.js |
2 | // version: 0.3.2 | 2 | // version: 0.3.3 |
3 | // author: Michael Richards | 3 | // author: Michael Richards |
4 | // license: MIT | 4 | // license: MIT |
5 | (function() { | 5 | (function() { |
6 | var Rivets, attributeBinding, bindEvent, eventBinding, getInputValue, iterationBinding, rivets, unbindEvent, | 6 | var Rivets, attributeBinding, bindEvent, classBinding, eventBinding, getInputValue, iterationBinding, rivets, unbindEvent, |
7 | __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, | 7 | __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, |
8 | __slice = [].slice, | 8 | __slice = [].slice, |
9 | __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; | 9 | __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; |
... | @@ -38,6 +38,8 @@ | ... | @@ -38,6 +38,8 @@ |
38 | switch (this.options.special) { | 38 | switch (this.options.special) { |
39 | case "event": | 39 | case "event": |
40 | return eventBinding(this.type); | 40 | return eventBinding(this.type); |
41 | case "class": | ||
42 | return classBinding(this.type); | ||
41 | case "iteration": | 43 | case "iteration": |
42 | return iterationBinding(this.type); | 44 | return iterationBinding(this.type); |
43 | default: | 45 | default: |
... | @@ -161,13 +163,14 @@ | ... | @@ -161,13 +163,14 @@ |
161 | }; | 163 | }; |
162 | 164 | ||
163 | View.prototype.build = function() { | 165 | View.prototype.build = function() { |
164 | var bindingRegExp, el, eventRegExp, iterationRegExp, iterator, node, parseNode, skipNodes, _i, _len, _ref, _results, | 166 | var bindingRegExp, classRegExp, el, eventRegExp, iterationRegExp, iterator, node, parseNode, skipNodes, _i, _len, _ref, _results, |
165 | _this = this; | 167 | _this = this; |
166 | this.bindings = []; | 168 | this.bindings = []; |
167 | skipNodes = []; | 169 | skipNodes = []; |
168 | iterator = null; | 170 | iterator = null; |
169 | bindingRegExp = this.bindingRegExp(); | 171 | bindingRegExp = this.bindingRegExp(); |
170 | eventRegExp = /^on-/; | 172 | eventRegExp = /^on-/; |
173 | classRegExp = /^class-/; | ||
171 | iterationRegExp = /^each-/; | 174 | iterationRegExp = /^each-/; |
172 | parseNode = function(node) { | 175 | parseNode = function(node) { |
173 | var a, attribute, binding, context, ctx, dependencies, keypath, model, n, options, path, pipe, pipes, splitPath, type, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref, _ref1, _ref2, _results; | 176 | var a, attribute, binding, context, ctx, dependencies, keypath, model, n, options, path, pipe, pipes, splitPath, type, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref, _ref1, _ref2, _results; |
... | @@ -230,6 +233,10 @@ | ... | @@ -230,6 +233,10 @@ |
230 | type = type.replace(eventRegExp, ''); | 233 | type = type.replace(eventRegExp, ''); |
231 | options.special = "event"; | 234 | options.special = "event"; |
232 | } | 235 | } |
236 | if (classRegExp.test(type)) { | ||
237 | type = type.replace(classRegExp, ''); | ||
238 | options.special = "class"; | ||
239 | } | ||
233 | if (iterationRegExp.test(type)) { | 240 | if (iterationRegExp.test(type)) { |
234 | type = type.replace(iterationRegExp, ''); | 241 | type = type.replace(iterationRegExp, ''); |
235 | options.special = "iteration"; | 242 | options.special = "iteration"; |
... | @@ -335,6 +342,17 @@ | ... | @@ -335,6 +342,17 @@ |
335 | }; | 342 | }; |
336 | }; | 343 | }; |
337 | 344 | ||
345 | classBinding = function(name) { | ||
346 | return function(el, value) { | ||
347 | var elClass, hasClass; | ||
348 | elClass = " " + el.className + " "; | ||
349 | hasClass = elClass.indexOf(" " + name + " ") !== -1; | ||
350 | if (!value === hasClass) { | ||
351 | return el.className = value ? "" + el.className + " " + name : elClass.replace(" " + name + " ", " ").trim(); | ||
352 | } | ||
353 | }; | ||
354 | }; | ||
355 | |||
338 | iterationBinding = function(name) { | 356 | iterationBinding = function(name) { |
339 | return function(el, collection, binding) { | 357 | return function(el, collection, binding) { |
340 | var data, item, itemEl, iteration, m, n, previous, _i, _j, _len, _len1, _ref, _ref1, _results; | 358 | var data, item, itemEl, iteration, m, n, previous, _i, _j, _len, _len1, _ref, _ref1, _results; | ... | ... |
1 | // rivets.js | 1 | // rivets.js |
2 | // version: 0.3.2 | 2 | // version: 0.3.3 |
3 | // author: Michael Richards | 3 | // author: Michael Richards |
4 | // license: MIT | 4 | // license: MIT |
5 | (function(){var a,b,c,d,e,f,g,h,i=function(a,b){return function(){return a.apply(b,arguments)}},j=[].slice,k=[].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 g(c,e,g,h,j){this.el=c,this.type=e,this.model=g,this.keypath=h,this.options=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.formattedValue=i(this.formattedValue,this),this.routine=function(){switch(this.options.special){case"event":return d(this.type);case"iteration":return f(this.type);default:return a.routines[this.type]||b(this.type)}}.call(this),this.formatters=this.options.formatters||[]}return g.prototype.bidirectionals=["value","checked","unchecked"],g.prototype.formattedValue=function(b){var c,d,e,f,g,h,i,k;h=this.formatters;for(f=0,g=h.length;f<g;f++)d=h[f],c=d.split(/\s+/),e=c.shift(),b=this.model[e]instanceof Function?(i=this.model)[e].apply(i,[b].concat(j.call(c))):a.formatters[e]?(k=a.formatters)[e].apply(k,[b].concat(j.call(c))):void 0;return b},g.prototype.set=function(a){return a=this.formattedValue(a),this.options.special==="event"?(this.routine(this.el,a,this.currentListener),this.currentListener=a):this.options.special==="iteration"?this.routine(this.el,a,this):(a instanceof Function&&(a=a.call(this.model)),this.routine(this.el,a))},g.prototype.bind=function(){var b,d,e,f,g,h,i,j=this;this.options.bypass?this.set(this.model[this.keypath]):(a.config.adapter.subscribe(this.model,this.keypath,this.set),a.config.preloadData&&this.set(a.config.adapter.read(this.model,this.keypath)));if((g=this.options.dependencies)!=null?g.length:void 0){this.dependencyCallbacks={},h=this.options.dependencies;for(e=0,f=h.length;e<f;e++)d=h[e],b=this.dependencyCallbacks[d]=function(b){return j.set(j.options.bypass?j.model[j.keypath]:a.config.adapter.read(j.model,j.keypath))},a.config.adapter.subscribe(this.model,d,b)}if(i=this.type,k.call(this.bidirectionals,i)>=0)return c(this.el,"change",this.publish)},g.prototype.publish=function(b){var c;return c=b.target||b.srcElement,a.config.adapter.publish(this.model,this.keypath,e(c))},g.prototype.unbind=function(){var b,c,d,e,f,g,h;if(!this.options.bypass){a.config.adapter.unsubscribe(this.model,this.keypath,this.set);if((f=this.options.dependencies)!=null?f.length:void 0){g=this.options.dependencies;for(d=0,e=g.length;d<e;d++)c=g[d],b=this.dependencyCallbacks[c],a.config.adapter.unsubscribe(this.model,c,b)}if(h=this.type,k.call(this.bidirectionals,h)>=0)return this.el.removeEventListener("change",this.publish)}},g}(),a.View=function(){function b(a,b){this.els=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.els.jquery||this.els instanceof Array||(this.els=[this.els]),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,c,d,e,f,g,h,i,j,l,m,n,o=this;this.bindings=[],i=[],f=null,b=this.bindingRegExp(),d=/^on-/,e=/^each-/,h=function(c){var g,h,j,l,m,n,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J;if(k.call(i,c)<0){G=c.attributes;for(y=0,C=G.length;y<C;y++){h=G[y];if(b.test(h.name)){x=h.name.replace(b,"");if(e.test(x)&&!o.models[x.replace(e,"")]){H=c.getElementsByTagName("*");for(z=0,D=H.length;z<D;z++)r=H[z],i.push(r);f=[h]}}}I=f||c.attributes,J=[];for(A=0,E=I.length;A<E;A++){h=I[A];if(b.test(h.name)){s={},x=h.name.replace(b,""),v=function(){var a,b,c,d;c=h.value.split("|"),d=[];for(a=0,b=c.length;a<b;a++)u=c[a],d.push(u.trim());return d}(),l=function(){var a,b,c,d;c=v.shift().split(">"),d=[];for(a=0,b=c.length;a<b;a++)m=c[a],d.push(m.trim());return d}(),t=l.shift(),w=t.split(/\.|:/),s.formatters=v,q=o.models[w.shift()],s.bypass=t.indexOf(":")!==-1,p=w.join();if(q){if(n=l.shift())s.dependencies=n.split(/\s+/);d.test(x)&&(x=x.replace(d,""),s.special="event"),e.test(x)&&(x=x.replace(e,""),s.special="iteration"),j=new a.Binding(c,x,q,p,s),j.view=o,o.bindings.push(j)}}if(f){for(B=0,F=f.length;B<F;B++)g=f[B],c.removeAttribute(g.name);J.push(f=null)}else J.push(void 0)}return J}},m=this.els,n=[];for(j=0,l=m.length;j<l;j++)c=m[j],h(c),n.push(function(){var a,b,d,e;d=c.getElementsByTagName("*"),e=[];for(a=0,b=d.length;a<b;a++)g=d[a],e.push(h(g));return e}());return n},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}(),c=function(a,b,c){return window.addEventListener?a.addEventListener(b,c,!1):(b="on"+b,a.attachEvent(b,c))},h=function(a,b,c){return window.removeEventListener?a.removeEventListener(b,c,!1):(b="on"+b,a.detachEvent(b,c))},e=function(a){switch(a.type){case"checkbox":return a.checked;default:return a.value}},d=function(a){return function(b,d,e){d&&c(b,a,d);if(e)return h(b,a,e)}},f=function(a){return function(b,c,d){var e,f,h,i,j,k,l,m,n,o,p,q,r,s;if(d.iterated!=null){q=d.iterated;for(m=0,o=q.length;m<o;m++)i=q[m],i.view.unbind(),i.el.parentNode.removeChild(i.el)}else d.marker=document.createComment(" rivets: each-"+a+" "),b.parentNode.insertBefore(d.marker,b),b.parentNode.removeChild(b);d.iterated=[],s=[];for(n=0,p=c.length;n<p;n++){f=c[n],e={},r=d.view.models;for(k in r)j=r[k],e[k]=j;e[a]=f,h=b.cloneNode(!0),l=d.iterated[d.iterated.length-1]||d.marker,d.marker.parentNode.insertBefore(h,l.nextSibling),s.push(d.iterated.push({el:h,view:g.bind(h,e)}))}return s}},b=function(a){return function(b,c){return c?b.setAttribute(a,c):b.removeAttribute(a)}},a.routines={enabled:function(a,b){return a.disabled=!b},disabled:function(a,b){return a.disabled=!!b},checked:function(a,b){return a.type==="radio"?a.checked=a.value===b:a.checked=!!b},unchecked:function(a,b){return a.type==="radio"?a.checked=a.value!==b:a.checked=!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!=null?b:""},value:function(a,b){return a.value=b!=null?b:""},text:function(a,b){return a.innerText!=null?a.innerText=b!=null?b:"":a.textContent=b!=null?b:""}},a.config={preloadData:!0},a.formatters={},g={routines:a.routines,formatters:a.formatters,config:a.config,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},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 |
5 | (function(){var a,b,c,d,e,f,g,h,i,j=function(a,b){return function(){return a.apply(b,arguments)}},k=[].slice,l=[].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 h(c,f,h,i,k){this.el=c,this.type=f,this.model=h,this.keypath=i,this.options=k!=null?k:{},this.unbind=j(this.unbind,this),this.publish=j(this.publish,this),this.bind=j(this.bind,this),this.set=j(this.set,this),this.formattedValue=j(this.formattedValue,this),this.routine=function(){switch(this.options.special){case"event":return e(this.type);case"class":return d(this.type);case"iteration":return g(this.type);default:return a.routines[this.type]||b(this.type)}}.call(this),this.formatters=this.options.formatters||[]}return h.prototype.bidirectionals=["value","checked","unchecked"],h.prototype.formattedValue=function(b){var c,d,e,f,g,h,i,j;h=this.formatters;for(f=0,g=h.length;f<g;f++)d=h[f],c=d.split(/\s+/),e=c.shift(),b=this.model[e]instanceof Function?(i=this.model)[e].apply(i,[b].concat(k.call(c))):a.formatters[e]?(j=a.formatters)[e].apply(j,[b].concat(k.call(c))):void 0;return b},h.prototype.set=function(a){return a=this.formattedValue(a),this.options.special==="event"?(this.routine(this.el,a,this.currentListener),this.currentListener=a):this.options.special==="iteration"?this.routine(this.el,a,this):(a instanceof Function&&(a=a.call(this.model)),this.routine(this.el,a))},h.prototype.bind=function(){var b,d,e,f,g,h,i,j=this;this.options.bypass?this.set(this.model[this.keypath]):(a.config.adapter.subscribe(this.model,this.keypath,this.set),a.config.preloadData&&this.set(a.config.adapter.read(this.model,this.keypath)));if((g=this.options.dependencies)!=null?g.length:void 0){this.dependencyCallbacks={},h=this.options.dependencies;for(e=0,f=h.length;e<f;e++)d=h[e],b=this.dependencyCallbacks[d]=function(b){return j.set(j.options.bypass?j.model[j.keypath]:a.config.adapter.read(j.model,j.keypath))},a.config.adapter.subscribe(this.model,d,b)}if(i=this.type,l.call(this.bidirectionals,i)>=0)return c(this.el,"change",this.publish)},h.prototype.publish=function(b){var c;return c=b.target||b.srcElement,a.config.adapter.publish(this.model,this.keypath,f(c))},h.prototype.unbind=function(){var b,c,d,e,f,g,h;if(!this.options.bypass){a.config.adapter.unsubscribe(this.model,this.keypath,this.set);if((f=this.options.dependencies)!=null?f.length:void 0){g=this.options.dependencies;for(d=0,e=g.length;d<e;d++)c=g[d],b=this.dependencyCallbacks[c],a.config.adapter.unsubscribe(this.model,c,b)}if(h=this.type,l.call(this.bidirectionals,h)>=0)return this.el.removeEventListener("change",this.publish)}},h}(),a.View=function(){function b(a,b){this.els=a,this.models=b,this.unbind=j(this.unbind,this),this.bind=j(this.bind,this),this.build=j(this.build,this),this.bindingRegExp=j(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),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,c,d,e,f,g,h,i,j,k,m,n,o,p=this;this.bindings=[],j=[],g=null,b=this.bindingRegExp(),e=/^on-/,c=/^class-/,f=/^each-/,i=function(d){var h,i,k,m,n,o,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K;if(l.call(j,d)<0){H=d.attributes;for(z=0,D=H.length;z<D;z++){i=H[z];if(b.test(i.name)){y=i.name.replace(b,"");if(f.test(y)&&!p.models[y.replace(f,"")]){I=d.getElementsByTagName("*");for(A=0,E=I.length;A<E;A++)s=I[A],j.push(s);g=[i]}}}J=g||d.attributes,K=[];for(B=0,F=J.length;B<F;B++){i=J[B];if(b.test(i.name)){t={},y=i.name.replace(b,""),w=function(){var a,b,c,d;c=i.value.split("|"),d=[];for(a=0,b=c.length;a<b;a++)v=c[a],d.push(v.trim());return d}(),m=function(){var a,b,c,d;c=w.shift().split(">"),d=[];for(a=0,b=c.length;a<b;a++)n=c[a],d.push(n.trim());return d}(),u=m.shift(),x=u.split(/\.|:/),t.formatters=w,r=p.models[x.shift()],t.bypass=u.indexOf(":")!==-1,q=x.join();if(r){if(o=m.shift())t.dependencies=o.split(/\s+/);e.test(y)&&(y=y.replace(e,""),t.special="event"),c.test(y)&&(y=y.replace(c,""),t.special="class"),f.test(y)&&(y=y.replace(f,""),t.special="iteration"),k=new a.Binding(d,y,r,q,t),k.view=p,p.bindings.push(k)}}if(g){for(C=0,G=g.length;C<G;C++)h=g[C],d.removeAttribute(h.name);K.push(g=null)}else K.push(void 0)}return K}},n=this.els,o=[];for(k=0,m=n.length;k<m;k++)d=n[k],i(d),o.push(function(){var a,b,c,e;c=d.getElementsByTagName("*"),e=[];for(a=0,b=c.length;a<b;a++)h=c[a],e.push(i(h));return e}());return o},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}(),c=function(a,b,c){return window.addEventListener?a.addEventListener(b,c,!1):(b="on"+b,a.attachEvent(b,c))},i=function(a,b,c){return window.removeEventListener?a.removeEventListener(b,c,!1):(b="on"+b,a.detachEvent(b,c))},f=function(a){switch(a.type){case"checkbox":return a.checked;default:return a.value}},e=function(a){return function(b,d,e){d&&c(b,a,d);if(e)return i(b,a,e)}},d=function(a){return function(b,c){var d,e;d=" "+b.className+" ",e=d.indexOf(" "+a+" ")!==-1;if(!c===e)return b.className=c?""+b.className+" "+a:d.replace(" "+a+" "," ").trim()}},g=function(a){return function(b,c,d){var e,f,g,i,j,k,l,m,n,o,p,q,r,s;if(d.iterated!=null){q=d.iterated;for(m=0,o=q.length;m<o;m++)i=q[m],i.view.unbind(),i.el.parentNode.removeChild(i.el)}else d.marker=document.createComment(" rivets: each-"+a+" "),b.parentNode.insertBefore(d.marker,b),b.parentNode.removeChild(b);d.iterated=[],s=[];for(n=0,p=c.length;n<p;n++){f=c[n],e={},r=d.view.models;for(k in r)j=r[k],e[k]=j;e[a]=f,g=b.cloneNode(!0),l=d.iterated[d.iterated.length-1]||d.marker,d.marker.parentNode.insertBefore(g,l.nextSibling),s.push(d.iterated.push({el:g,view:h.bind(g,e)}))}return s}},b=function(a){return function(b,c){return c?b.setAttribute(a,c):b.removeAttribute(a)}},a.routines={enabled:function(a,b){return a.disabled=!b},disabled:function(a,b){return a.disabled=!!b},checked:function(a,b){return a.type==="radio"?a.checked=a.value===b:a.checked=!!b},unchecked:function(a,b){return a.type==="radio"?a.checked=a.value!==b:a.checked=!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!=null?b:""},value:function(a,b){return a.value=b!=null?b:""},text:function(a,b){return a.innerText!=null?a.innerText=b!=null?b:"":a.textContent=b!=null?b:""}},a.config={preloadData:!0},a.formatters={},h={routines:a.routines,formatters:a.formatters,config:a.config,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},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=h:this.rivets=h}).call(this); | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
1 | { | 1 | { |
2 | "name" : "rivets", | 2 | "name" : "rivets", |
3 | "description" : "Declarative data binding facility.", | 3 | "description" : "Declarative data binding facility.", |
4 | "version" : "0.3.2", | 4 | "version" : "0.3.3", |
5 | "author" : "Michael Richards", | 5 | "author" : "Michael Richards", |
6 | "url" : "http://rivetsjs.com", | 6 | "url" : "http://rivetsjs.com", |
7 | "main" : "./lib/rivets.js", | 7 | "main" : "./lib/rivets.js", | ... | ... |
-
Please register or sign in to post a comment