Merge branch 'master' into text-templates
Showing
5 changed files
with
95 additions
and
32 deletions
... | @@ -2,7 +2,7 @@ | ... | @@ -2,7 +2,7 @@ |
2 | "name": "rivets", | 2 | "name": "rivets", |
3 | "repo": "mikeric/rivets", | 3 | "repo": "mikeric/rivets", |
4 | "description": "Declarative data binding facility.", | 4 | "description": "Declarative data binding facility.", |
5 | "version": "0.5.7", | 5 | "version": "0.5.8", |
6 | "keywords": ["data binding", "templating"], | 6 | "keywords": ["data binding", "templating"], |
7 | "scripts": ["dist/rivets.js"], | 7 | "scripts": ["dist/rivets.js"], |
8 | "main": "dist/rivets.js", | 8 | "main": "dist/rivets.js", | ... | ... |
1 | // Rivets.js | 1 | // Rivets.js |
2 | // version: 0.5.7 | 2 | // version: 0.5.8 |
3 | // author: Michael Richards | 3 | // author: Michael Richards |
4 | // license: MIT | 4 | // license: MIT |
5 | (function() { | 5 | (function() { |
... | @@ -173,10 +173,31 @@ | ... | @@ -173,10 +173,31 @@ |
173 | } | 173 | } |
174 | }; | 174 | }; |
175 | 175 | ||
176 | Binding.prototype.update = function() { | 176 | Binding.prototype.update = function(models) { |
177 | this.unbind(); | 177 | var _ref; |
178 | this.model = this.key ? this.view.models[this.key] : this.view.models; | 178 | |
179 | return this.bind(); | 179 | if (models == null) { |
180 | models = {}; | ||
181 | } | ||
182 | if (this.key) { | ||
183 | if (models[this.key]) { | ||
184 | if (!this.options.bypass) { | ||
185 | this.view.config.adapter.unsubscribe(this.model, this.keypath, this.sync); | ||
186 | } | ||
187 | this.model = models[this.key]; | ||
188 | if (this.options.bypass) { | ||
189 | this.sync(); | ||
190 | } else { | ||
191 | this.view.config.adapter.subscribe(this.model, this.keypath, this.sync); | ||
192 | if (this.view.config.preloadData) { | ||
193 | this.sync(); | ||
194 | } | ||
195 | } | ||
196 | } | ||
197 | } else { | ||
198 | this.sync(); | ||
199 | } | ||
200 | return (_ref = this.binder.update) != null ? _ref.call(this, models) : void 0; | ||
180 | }; | 201 | }; |
181 | 202 | ||
182 | return Binding; | 203 | return Binding; |
... | @@ -404,28 +425,20 @@ | ... | @@ -404,28 +425,20 @@ |
404 | }; | 425 | }; |
405 | 426 | ||
406 | View.prototype.update = function(models) { | 427 | View.prototype.update = function(models) { |
407 | var binding, key, model, _results; | 428 | var binding, key, model, _i, _len, _ref, _results; |
408 | 429 | ||
409 | if (models == null) { | 430 | if (models == null) { |
410 | models = {}; | 431 | models = {}; |
411 | } | 432 | } |
412 | _results = []; | ||
413 | for (key in models) { | 433 | for (key in models) { |
414 | model = models[key]; | 434 | model = models[key]; |
415 | this.models[key] = model; | 435 | this.models[key] = model; |
416 | _results.push((function() { | 436 | } |
417 | var _i, _len, _ref, _results1; | 437 | _ref = this.bindings; |
418 | 438 | _results = []; | |
419 | _ref = this.select(function(b) { | 439 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
420 | return b.key === key; | 440 | binding = _ref[_i]; |
421 | }); | 441 | _results.push(binding.update(models)); |
422 | _results1 = []; | ||
423 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
424 | binding = _ref[_i]; | ||
425 | _results1.push(binding.update()); | ||
426 | } | ||
427 | return _results1; | ||
428 | }).call(this)); | ||
429 | } | 442 | } |
430 | return _results; | 443 | return _results; |
431 | }; | 444 | }; |
... | @@ -631,6 +644,9 @@ | ... | @@ -631,6 +644,9 @@ |
631 | return delete this.nested; | 644 | return delete this.nested; |
632 | } | 645 | } |
633 | } | 646 | } |
647 | }, | ||
648 | update: function(models) { | ||
649 | return this.nested.update(models); | ||
634 | } | 650 | } |
635 | }, | 651 | }, |
636 | unless: { | 652 | unless: { |
... | @@ -643,6 +659,9 @@ | ... | @@ -643,6 +659,9 @@ |
643 | }, | 659 | }, |
644 | routine: function(el, value) { | 660 | routine: function(el, value) { |
645 | return Rivets.binders["if"].routine.call(this, el, !value); | 661 | return Rivets.binders["if"].routine.call(this, el, !value); |
662 | }, | ||
663 | update: function(models) { | ||
664 | return Rivets.binders["if"].update.call(this, models); | ||
646 | } | 665 | } |
647 | }, | 666 | }, |
648 | "on-*": { | 667 | "on-*": { |
... | @@ -737,6 +756,24 @@ | ... | @@ -737,6 +756,24 @@ |
737 | } | 756 | } |
738 | } | 757 | } |
739 | return _results; | 758 | return _results; |
759 | }, | ||
760 | update: function(models) { | ||
761 | var data, key, model, view, _i, _len, _ref, _results; | ||
762 | |||
763 | data = {}; | ||
764 | for (key in models) { | ||
765 | model = models[key]; | ||
766 | if (key !== this.args[0]) { | ||
767 | data[key] = model; | ||
768 | } | ||
769 | } | ||
770 | _ref = this.iterated; | ||
771 | _results = []; | ||
772 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
773 | view = _ref[_i]; | ||
774 | _results.push(view.update(data)); | ||
775 | } | ||
776 | return _results; | ||
740 | } | 777 | } |
741 | }, | 778 | }, |
742 | "class-*": function(el, value) { | 779 | "class-*": function(el, value) { | ... | ... |
1 | // Rivets.js | 1 | // Rivets.js |
2 | // version: 0.5.7 | 2 | // version: 0.5.8 |
3 | // author: Michael Richards | 3 | // author: Michael Richards |
4 | // license: MIT | 4 | // license: MIT |
5 | (function(){var t,i=function(t,i){return function(){return t.apply(i,arguments)}},e=[].slice,n=[].indexOf||function(t){for(var i=0,e=this.length;e>i;i++)if(i in this&&this[i]===t)return i;return-1};t={},String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),t.Binding=function(){function n(t,e,n,s,r,h){var o,l,u,d;if(this.view=t,this.el=e,this.type=n,this.key=s,this.keypath=r,this.options=null!=h?h:{},this.update=i(this.update,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.set=i(this.set,this),this.eventHandler=i(this.eventHandler,this),this.formattedValue=i(this.formattedValue,this),!(this.binder=this.view.binders[n])){d=this.view.binders;for(o in d)u=d[o],"*"!==o&&-1!==o.indexOf("*")&&(l=RegExp("^"+o.replace("*",".+")+"$"),l.test(n)&&(this.binder=u,this.args=RegExp("^"+o.replace("*","(.+)")+"$").exec(n),this.args.shift()))}this.binder||(this.binder=this.view.binders["*"]),this.binder instanceof Function&&(this.binder={routine:this.binder}),this.formatters=this.options.formatters||[],this.model=this.key?this.view.models[this.key]:this.view.models}return n.prototype.formattedValue=function(t){var i,n,s,r,h,o;for(o=this.formatters,r=0,h=o.length;h>r;r++)n=o[r],i=n.split(/\s+/),s=i.shift(),n=this.model[s]instanceof Function?this.model[s]:this.view.formatters[s],(null!=n?n.read:void 0)instanceof Function?t=n.read.apply(n,[t].concat(e.call(i))):n instanceof Function&&(t=n.apply(null,[t].concat(e.call(i))));return t},n.prototype.eventHandler=function(t){var i,e;return e=(i=this).view.config.handler,function(n){return e.call(t,this,n,i)}},n.prototype.set=function(t){var i;return t=t instanceof Function&&!this.binder["function"]?this.formattedValue(t.call(this.model)):this.formattedValue(t),null!=(i=this.binder.routine)?i.call(this,this.el,t):void 0},n.prototype.sync=function(){return this.set(this.options.bypass?this.model[this.keypath]:this.view.config.adapter.read(this.model,this.keypath))},n.prototype.publish=function(){var i,n,s,r,h,o,l,u,d;for(r=t.Util.getInputValue(this.el),l=this.formatters.slice(0).reverse(),h=0,o=l.length;o>h;h++)n=l[h],i=n.split(/\s+/),s=i.shift(),(null!=(u=this.view.formatters[s])?u.publish:void 0)&&(r=(d=this.view.formatters[s]).publish.apply(d,[r].concat(e.call(i))));return this.view.config.adapter.publish(this.model,this.keypath,r)},n.prototype.bind=function(){var t,i,e,n,s,r,h,o,l;if(null!=(r=this.binder.bind)&&r.call(this,this.el),this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync()),null!=(h=this.options.dependencies)?h.length:void 0){for(o=this.options.dependencies,l=[],n=0,s=o.length;s>n;n++)t=o[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),l.push(this.view.config.adapter.subscribe(e,i,this.sync));return l}},n.prototype.unbind=function(){var t,i,e,n,s,r,h,o,l;if(null!=(r=this.binder.unbind)&&r.call(this,this.el),this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),null!=(h=this.options.dependencies)?h.length:void 0){for(o=this.options.dependencies,l=[],n=0,s=o.length;s>n;n++)t=o[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),l.push(this.view.config.adapter.unsubscribe(e,i,this.sync));return l}},n.prototype.update=function(){return this.unbind(),this.model=this.key?this.view.models[this.key]:this.view.models,this.bind()},n}(),t.View=function(){function e(e,n,s){var r,h,o,l,u,d,a,c,f,p;for(this.els=e,this.models=n,this.options=null!=s?s:{},this.update=i(this.update,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.select=i(this.select,this),this.build=i(this.build,this),this.bindingRegExp=i(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),a=["config","binders","formatters"],u=0,d=a.length;d>u;u++){if(h=a[u],this[h]={},this.options[h]){c=this.options[h];for(r in c)o=c[r],this[h][r]=o}f=t[h];for(r in f)o=f[r],null==(p=(l=this[h])[r])&&(l[r]=o)}this.build()}return e.prototype.bindingRegExp=function(){var t;return t=this.config.prefix,t?RegExp("^data-"+t+"-"):/^data-/},e.prototype.build=function(){var i,e,s,r,h,o,l,u,d,a,c,f=this;for(this.bindings=[],h=[],i=this.bindingRegExp(),r=function(e){var s,r,o,l,u,d,a,c,p,b,v,g,m,y,w,k,x,E,j,N,U,V,B,Q,R,A,C,F;if(0>n.call(h,e)){for(R=e.attributes,j=0,V=R.length;V>j;j++)if(s=R[j],i.test(s.name)){if(x=s.name.replace(i,""),!(o=f.binders[x])){A=f.binders;for(a in A)E=A[a],"*"!==a&&-1!==a.indexOf("*")&&(w=RegExp("^"+a.replace("*",".+")+"$"),w.test(x)&&(o=E))}if(o||(o=f.binders["*"]),o.block){for(C=e.getElementsByTagName("*"),N=0,B=C.length;B>N;N++)b=C[N],h.push(b);r=[s]}}for(F=r||e.attributes,U=0,Q=F.length;Q>U;U++)s=F[U],i.test(s.name)&&(v={},x=s.name.replace(i,""),y=function(){var t,i,e,n;for(e=s.value.split("|"),n=[],t=0,i=e.length;i>t;t++)m=e[t],n.push(m.trim());return n}(),l=function(){var t,i,e,n;for(e=y.shift().split("<"),n=[],t=0,i=e.length;i>t;t++)u=e[t],n.push(u.trim());return n}(),g=l.shift(),k=g.split(/\.|:/),v.formatters=y,v.bypass=-1!==g.indexOf(":"),k[0]?c=k.shift():(c=null,k.shift()),p=k.join("."),c&&null==f.models[c]||((d=l.shift())&&(v.dependencies=d.split(/\s+/)),f.bindings.push(new t.Binding(f,e,x,c,p,v))));r&&(r=null)}},a=this.els,o=0,u=a.length;u>o;o++)for(e=a[o],r(e),c=e.getElementsByTagName("*"),l=0,d=c.length;d>l;l++)s=c[l],null!=s.attributes&&r(s)},e.prototype.select=function(t){var i,e,n,s,r;for(s=this.bindings,r=[],e=0,n=s.length;n>e;e++)i=s[e],t(i)&&r.push(i);return r},e.prototype.bind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.bind());return s},e.prototype.unbind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s},e.prototype.sync=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.sync());return s},e.prototype.publish=function(){var t,i,e,n,s;for(n=this.select(function(t){return t.binder.publishes}),s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.publish());return s},e.prototype.update=function(t){var i,e,n,s;null==t&&(t={}),s=[];for(e in t)n=t[e],this.models[e]=n,s.push(function(){var t,n,s,r;for(s=this.select(function(t){return t.key===e}),r=[],t=0,n=s.length;n>t;t++)i=s[t],r.push(i.update());return r}.call(this));return s},e}(),t.Util={bindEvent:function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.on?t.on(i,e):t.bind(i,e)):null!=window.addEventListener?t.addEventListener(i,e,!1):(i="on"+i,t.attachEvent(i,e))},unbindEvent:function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.off?t.off(i,e):t.unbind(i,e)):null!=window.removeEventListener?t.removeEventListener(i,e,!1):(i="on"+i,t.detachEvent(i,e))},getInputValue:function(t){var i,e,n,s;if(null!=window.jQuery)switch(t=jQuery(t),t[0].type){case"checkbox":return t.is(":checked");default:return t.val()}else switch(t.type){case"checkbox":return t.checked;case"select-multiple":for(s=[],e=0,n=t.length;n>e;e++)i=t[e],i.selected&&s.push(i.value);return s;default:return t.value}}},t.binders={enabled:function(t,i){return t.disabled=!i},disabled:function(t,i){return t.disabled=!!i},checked:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)===(null!=i?""+i:void 0):!!i}},unchecked:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)!==(null!=i?""+i:void 0):!i}},show:function(t,i){return t.style.display=i?"":"none"},hide:function(t,i){return t.style.display=i?"none":""},html:function(t,i){return t.innerHTML=null!=i?i:""},value:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e,s,r,h,o,l,u;if(null!=window.jQuery){if(t=jQuery(t),(null!=i?""+i:void 0)!==(null!=(h=t.val())?""+h:void 0))return t.val(null!=i?i:"")}else if("select-multiple"===t.type){if(null!=i){for(u=[],s=0,r=t.length;r>s;s++)e=t[s],u.push(e.selected=(o=e.value,n.call(i,o)>=0));return u}}else if((null!=i?""+i:void 0)!==(null!=(l=t.value)?""+l:void 0))return t.value=null!=i?i:""}},text:function(t,i){return null!=t.innerText?t.innerText=null!=i?i:"":t.textContent=null!=i?i:""},"if":{block:!0,bind:function(t){var i,e;return null==this.marker?(i=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),e=t.getAttribute(i),this.marker=document.createComment(" rivets: "+this.type+" "+e+" "),t.removeAttribute(i),t.parentNode.insertBefore(this.marker,t),t.parentNode.removeChild(t)):void 0},unbind:function(){var t;return null!=(t=this.nested)?t.unbind():void 0},routine:function(i,e){var n,s,r,h,o;if(e===(null==this.nested)){if(e){r={},o=this.view.models;for(n in o)s=o[n],r[n]=s;return h={binders:this.view.options.binders,formatters:this.view.options.formatters,config:this.view.options.config},(this.nested=new t.View(i,r,h)).bind(),this.marker.parentNode.insertBefore(i,this.marker.nextSibling)}return i.parentNode.removeChild(i),this.nested.unbind(),delete this.nested}}},unless:{block:!0,bind:function(i){return t.binders["if"].bind.call(this,i)},unbind:function(){return t.binders["if"].unbind.call(this)},routine:function(i,e){return t.binders["if"].routine.call(this,i,!e)}},"on-*":{"function":!0,unbind:function(i){return this.handler?t.Util.unbindEvent(i,this.args[0],this.handler):void 0},routine:function(i,e){return this.handler&&t.Util.unbindEvent(i,this.args[0],this.handler),t.Util.bindEvent(i,this.args[0],this.handler=this.eventHandler(e))}},"each-*":{block:!0,bind:function(t){var i;return null==this.marker?(i=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],t.removeAttribute(i),t.parentNode.insertBefore(this.marker,t),t.parentNode.removeChild(t)):void 0},unbind:function(){var t,i,e,n,s;if(null!=this.iterated){for(n=this.iterated,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s}},routine:function(i,e){var n,s,r,h,o,l,u,d,a,c,f,p,b,v,g,m,y,w,k,x,E;if(u=this.args[0],e=e||[],this.iterated.length>e.length)for(y=Array(this.iterated.length-e.length),b=0,g=y.length;g>b;b++)s=y[b],p=this.iterated.pop(),p.unbind(),this.marker.parentNode.removeChild(p.els[0]);for(E=[],r=v=0,m=e.length;m>v;r=++v)if(l=e[r],n={},n[u]=l,null==this.iterated[r]){w=this.view.models;for(o in w)l=w[o],null==(k=n[o])&&(n[o]=l);a=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,d={binders:this.view.options.binders,formatters:this.view.options.formatters,config:{}},x=this.view.options.config;for(h in x)f=x[h],d.config[h]=f;d.config.preloadData=!0,c=i.cloneNode(!0),p=new t.View(c,n,d),p.bind(),this.iterated.push(p),E.push(this.marker.parentNode.insertBefore(c,a.nextSibling))}else this.iterated[r].models[u]!==l?E.push(this.iterated[r].update(n)):E.push(void 0);return E}},"class-*":function(t,i){var e;return e=" "+t.className+" ",!i==(-1!==e.indexOf(" "+this.args[0]+" "))?t.className=i?""+t.className+" "+this.args[0]:e.replace(" "+this.args[0]+" "," ").trim():void 0},"*":function(t,i){return i?t.setAttribute(this.type,i):t.removeAttribute(this.type)}},t.config={preloadData:!0,handler:function(t,i,e){return this.call(t,i,e.view.models)}},t.formatters={},t.factory=function(i){return i.binders=t.binders,i.formatters=t.formatters,i.config=t.config,i.configure=function(i){var e,n;null==i&&(i={});for(e in i)n=i[e],t.config[e]=n},i.bind=function(i,e,n){var s;return null==e&&(e={}),null==n&&(n={}),s=new t.View(i,e,n),s.bind(),s}},"object"==typeof exports?t.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(i){return t.factory(this.rivets=i),i}):t.factory(this.rivets={})}).call(this); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
5 | (function(){var t,i=function(t,i){return function(){return t.apply(i,arguments)}},e=[].slice,n=[].indexOf||function(t){for(var i=0,e=this.length;e>i;i++)if(i in this&&this[i]===t)return i;return-1};t={},String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),t.Binding=function(){function n(t,e,n,s,r,h){var o,u,l,a;if(this.view=t,this.el=e,this.type=n,this.key=s,this.keypath=r,this.options=null!=h?h:{},this.update=i(this.update,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.set=i(this.set,this),this.eventHandler=i(this.eventHandler,this),this.formattedValue=i(this.formattedValue,this),!(this.binder=this.view.binders[n])){a=this.view.binders;for(o in a)l=a[o],"*"!==o&&-1!==o.indexOf("*")&&(u=RegExp("^"+o.replace("*",".+")+"$"),u.test(n)&&(this.binder=l,this.args=RegExp("^"+o.replace("*","(.+)")+"$").exec(n),this.args.shift()))}this.binder||(this.binder=this.view.binders["*"]),this.binder instanceof Function&&(this.binder={routine:this.binder}),this.formatters=this.options.formatters||[],this.model=this.key?this.view.models[this.key]:this.view.models}return n.prototype.formattedValue=function(t){var i,n,s,r,h,o;for(o=this.formatters,r=0,h=o.length;h>r;r++)n=o[r],i=n.split(/\s+/),s=i.shift(),n=this.model[s]instanceof Function?this.model[s]:this.view.formatters[s],(null!=n?n.read:void 0)instanceof Function?t=n.read.apply(n,[t].concat(e.call(i))):n instanceof Function&&(t=n.apply(null,[t].concat(e.call(i))));return t},n.prototype.eventHandler=function(t){var i,e;return e=(i=this).view.config.handler,function(n){return e.call(t,this,n,i)}},n.prototype.set=function(t){var i;return t=t instanceof Function&&!this.binder["function"]?this.formattedValue(t.call(this.model)):this.formattedValue(t),null!=(i=this.binder.routine)?i.call(this,this.el,t):void 0},n.prototype.sync=function(){return this.set(this.options.bypass?this.model[this.keypath]:this.view.config.adapter.read(this.model,this.keypath))},n.prototype.publish=function(){var i,n,s,r,h,o,u,l,a;for(r=t.Util.getInputValue(this.el),u=this.formatters.slice(0).reverse(),h=0,o=u.length;o>h;h++)n=u[h],i=n.split(/\s+/),s=i.shift(),(null!=(l=this.view.formatters[s])?l.publish:void 0)&&(r=(a=this.view.formatters[s]).publish.apply(a,[r].concat(e.call(i))));return this.view.config.adapter.publish(this.model,this.keypath,r)},n.prototype.bind=function(){var t,i,e,n,s,r,h,o,u;if(null!=(r=this.binder.bind)&&r.call(this,this.el),this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync()),null!=(h=this.options.dependencies)?h.length:void 0){for(o=this.options.dependencies,u=[],n=0,s=o.length;s>n;n++)t=o[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),u.push(this.view.config.adapter.subscribe(e,i,this.sync));return u}},n.prototype.unbind=function(){var t,i,e,n,s,r,h,o,u;if(null!=(r=this.binder.unbind)&&r.call(this,this.el),this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),null!=(h=this.options.dependencies)?h.length:void 0){for(o=this.options.dependencies,u=[],n=0,s=o.length;s>n;n++)t=o[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),u.push(this.view.config.adapter.unsubscribe(e,i,this.sync));return u}},n.prototype.update=function(t){var i;return null==t&&(t={}),this.key?t[this.key]&&(this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),this.model=t[this.key],this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync())):this.sync(),null!=(i=this.binder.update)?i.call(this,t):void 0},n}(),t.View=function(){function e(e,n,s){var r,h,o,u,l,a,d,c,f,p;for(this.els=e,this.models=n,this.options=null!=s?s:{},this.update=i(this.update,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.select=i(this.select,this),this.build=i(this.build,this),this.bindingRegExp=i(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),d=["config","binders","formatters"],l=0,a=d.length;a>l;l++){if(h=d[l],this[h]={},this.options[h]){c=this.options[h];for(r in c)o=c[r],this[h][r]=o}f=t[h];for(r in f)o=f[r],null==(p=(u=this[h])[r])&&(u[r]=o)}this.build()}return e.prototype.bindingRegExp=function(){var t;return t=this.config.prefix,t?RegExp("^data-"+t+"-"):/^data-/},e.prototype.build=function(){var i,e,s,r,h,o,u,l,a,d,c,f=this;for(this.bindings=[],h=[],i=this.bindingRegExp(),r=function(e){var s,r,o,u,l,a,d,c,p,b,v,g,y,m,w,k,x,E,j,N,U,V,B,Q,R,A,C,F;if(0>n.call(h,e)){for(R=e.attributes,j=0,V=R.length;V>j;j++)if(s=R[j],i.test(s.name)){if(x=s.name.replace(i,""),!(o=f.binders[x])){A=f.binders;for(d in A)E=A[d],"*"!==d&&-1!==d.indexOf("*")&&(w=RegExp("^"+d.replace("*",".+")+"$"),w.test(x)&&(o=E))}if(o||(o=f.binders["*"]),o.block){for(C=e.getElementsByTagName("*"),N=0,B=C.length;B>N;N++)b=C[N],h.push(b);r=[s]}}for(F=r||e.attributes,U=0,Q=F.length;Q>U;U++)s=F[U],i.test(s.name)&&(v={},x=s.name.replace(i,""),m=function(){var t,i,e,n;for(e=s.value.split("|"),n=[],t=0,i=e.length;i>t;t++)y=e[t],n.push(y.trim());return n}(),u=function(){var t,i,e,n;for(e=m.shift().split("<"),n=[],t=0,i=e.length;i>t;t++)l=e[t],n.push(l.trim());return n}(),g=u.shift(),k=g.split(/\.|:/),v.formatters=m,v.bypass=-1!==g.indexOf(":"),k[0]?c=k.shift():(c=null,k.shift()),p=k.join("."),c&&null==f.models[c]||((a=u.shift())&&(v.dependencies=a.split(/\s+/)),f.bindings.push(new t.Binding(f,e,x,c,p,v))));r&&(r=null)}},d=this.els,o=0,l=d.length;l>o;o++)for(e=d[o],r(e),c=e.getElementsByTagName("*"),u=0,a=c.length;a>u;u++)s=c[u],null!=s.attributes&&r(s)},e.prototype.select=function(t){var i,e,n,s,r;for(s=this.bindings,r=[],e=0,n=s.length;n>e;e++)i=s[e],t(i)&&r.push(i);return r},e.prototype.bind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.bind());return s},e.prototype.unbind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s},e.prototype.sync=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.sync());return s},e.prototype.publish=function(){var t,i,e,n,s;for(n=this.select(function(t){return t.binder.publishes}),s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.publish());return s},e.prototype.update=function(t){var i,e,n,s,r,h,o;null==t&&(t={});for(e in t)n=t[e],this.models[e]=n;for(h=this.bindings,o=[],s=0,r=h.length;r>s;s++)i=h[s],o.push(i.update(t));return o},e}(),t.Util={bindEvent:function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.on?t.on(i,e):t.bind(i,e)):null!=window.addEventListener?t.addEventListener(i,e,!1):(i="on"+i,t.attachEvent(i,e))},unbindEvent:function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.off?t.off(i,e):t.unbind(i,e)):null!=window.removeEventListener?t.removeEventListener(i,e,!1):(i="on"+i,t.detachEvent(i,e))},getInputValue:function(t){var i,e,n,s;if(null!=window.jQuery)switch(t=jQuery(t),t[0].type){case"checkbox":return t.is(":checked");default:return t.val()}else switch(t.type){case"checkbox":return t.checked;case"select-multiple":for(s=[],e=0,n=t.length;n>e;e++)i=t[e],i.selected&&s.push(i.value);return s;default:return t.value}}},t.binders={enabled:function(t,i){return t.disabled=!i},disabled:function(t,i){return t.disabled=!!i},checked:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)===(null!=i?""+i:void 0):!!i}},unchecked:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)!==(null!=i?""+i:void 0):!i}},show:function(t,i){return t.style.display=i?"":"none"},hide:function(t,i){return t.style.display=i?"none":""},html:function(t,i){return t.innerHTML=null!=i?i:""},value:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e,s,r,h,o,u,l;if(null!=window.jQuery){if(t=jQuery(t),(null!=i?""+i:void 0)!==(null!=(h=t.val())?""+h:void 0))return t.val(null!=i?i:"")}else if("select-multiple"===t.type){if(null!=i){for(l=[],s=0,r=t.length;r>s;s++)e=t[s],l.push(e.selected=(o=e.value,n.call(i,o)>=0));return l}}else if((null!=i?""+i:void 0)!==(null!=(u=t.value)?""+u:void 0))return t.value=null!=i?i:""}},text:function(t,i){return null!=t.innerText?t.innerText=null!=i?i:"":t.textContent=null!=i?i:""},"if":{block:!0,bind:function(t){var i,e;return null==this.marker?(i=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),e=t.getAttribute(i),this.marker=document.createComment(" rivets: "+this.type+" "+e+" "),t.removeAttribute(i),t.parentNode.insertBefore(this.marker,t),t.parentNode.removeChild(t)):void 0},unbind:function(){var t;return null!=(t=this.nested)?t.unbind():void 0},routine:function(i,e){var n,s,r,h,o;if(e===(null==this.nested)){if(e){r={},o=this.view.models;for(n in o)s=o[n],r[n]=s;return h={binders:this.view.options.binders,formatters:this.view.options.formatters,config:this.view.options.config},(this.nested=new t.View(i,r,h)).bind(),this.marker.parentNode.insertBefore(i,this.marker.nextSibling)}return i.parentNode.removeChild(i),this.nested.unbind(),delete this.nested}},update:function(t){return this.nested.update(t)}},unless:{block:!0,bind:function(i){return t.binders["if"].bind.call(this,i)},unbind:function(){return t.binders["if"].unbind.call(this)},routine:function(i,e){return t.binders["if"].routine.call(this,i,!e)},update:function(i){return t.binders["if"].update.call(this,i)}},"on-*":{"function":!0,unbind:function(i){return this.handler?t.Util.unbindEvent(i,this.args[0],this.handler):void 0},routine:function(i,e){return this.handler&&t.Util.unbindEvent(i,this.args[0],this.handler),t.Util.bindEvent(i,this.args[0],this.handler=this.eventHandler(e))}},"each-*":{block:!0,bind:function(t){var i;return null==this.marker?(i=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],t.removeAttribute(i),t.parentNode.insertBefore(this.marker,t),t.parentNode.removeChild(t)):void 0},unbind:function(){var t,i,e,n,s;if(null!=this.iterated){for(n=this.iterated,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s}},routine:function(i,e){var n,s,r,h,o,u,l,a,d,c,f,p,b,v,g,y,m,w,k,x,E;if(l=this.args[0],e=e||[],this.iterated.length>e.length)for(m=Array(this.iterated.length-e.length),b=0,g=m.length;g>b;b++)s=m[b],p=this.iterated.pop(),p.unbind(),this.marker.parentNode.removeChild(p.els[0]);for(E=[],r=v=0,y=e.length;y>v;r=++v)if(u=e[r],n={},n[l]=u,null==this.iterated[r]){w=this.view.models;for(o in w)u=w[o],null==(k=n[o])&&(n[o]=u);d=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,a={binders:this.view.options.binders,formatters:this.view.options.formatters,config:{}},x=this.view.options.config;for(h in x)f=x[h],a.config[h]=f;a.config.preloadData=!0,c=i.cloneNode(!0),p=new t.View(c,n,a),p.bind(),this.iterated.push(p),E.push(this.marker.parentNode.insertBefore(c,d.nextSibling))}else this.iterated[r].models[l]!==u?E.push(this.iterated[r].update(n)):E.push(void 0);return E},update:function(t){var i,e,n,s,r,h,o,u;i={};for(e in t)n=t[e],e!==this.args[0]&&(i[e]=n);for(o=this.iterated,u=[],r=0,h=o.length;h>r;r++)s=o[r],u.push(s.update(i));return u}},"class-*":function(t,i){var e;return e=" "+t.className+" ",!i==(-1!==e.indexOf(" "+this.args[0]+" "))?t.className=i?""+t.className+" "+this.args[0]:e.replace(" "+this.args[0]+" "," ").trim():void 0},"*":function(t,i){return i?t.setAttribute(this.type,i):t.removeAttribute(this.type)}},t.config={preloadData:!0,handler:function(t,i,e){return this.call(t,i,e.view.models)}},t.formatters={},t.factory=function(i){return i.binders=t.binders,i.formatters=t.formatters,i.config=t.config,i.configure=function(i){var e,n;null==i&&(i={});for(e in i)n=i[e],t.config[e]=n},i.bind=function(i,e,n){var s;return null==e&&(e={}),null==n&&(n={}),s=new t.View(i,e,n),s.bind(),s}},"object"==typeof exports?t.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(i){return t.factory(this.rivets=i),i}):t.factory(this.rivets={})}).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.5.7", | 4 | "version": "0.5.8", |
5 | "author": "Michael Richards", | 5 | "author": "Michael Richards", |
6 | "url": "http://rivetsjs.com", | 6 | "url": "http://rivetsjs.com", |
7 | "main": "./dist/rivets.js", | 7 | "main": "./dist/rivets.js", | ... | ... |
1 | # Rivets.js | 1 | # Rivets.js |
2 | # ========= | 2 | # ========= |
3 | 3 | ||
4 | # > version: 0.5.7 | 4 | # > version: 0.5.8 |
5 | # > author: Michael Richards | 5 | # > author: Michael Richards |
6 | # > license: MIT | 6 | # > license: MIT |
7 | # > | 7 | # > |
... | @@ -137,10 +137,23 @@ class Rivets.Binding | ... | @@ -137,10 +137,23 @@ class Rivets.Binding |
137 | 137 | ||
138 | # Updates the binding's model from what is currently set on the view. Unbinds | 138 | # Updates the binding's model from what is currently set on the view. Unbinds |
139 | # the old model first and then re-binds with the new model. | 139 | # the old model first and then re-binds with the new model. |
140 | update: => | 140 | update: (models = {}) => |
141 | @unbind() | 141 | if @key |
142 | @model = if @key then @view.models[@key] else @view.models | 142 | if models[@key] |
143 | @bind() | 143 | unless @options.bypass |
144 | @view.config.adapter.unsubscribe @model, @keypath, @sync | ||
145 | |||
146 | @model = models[@key] | ||
147 | |||
148 | if @options.bypass | ||
149 | @sync() | ||
150 | else | ||
151 | @view.config.adapter.subscribe @model, @keypath, @sync | ||
152 | @sync() if @view.config.preloadData | ||
153 | else | ||
154 | @sync() | ||
155 | |||
156 | @binder.update?.call @, models | ||
144 | 157 | ||
145 | # Rivets.View | 158 | # Rivets.View |
146 | # ----------- | 159 | # ----------- |
... | @@ -263,9 +276,8 @@ class Rivets.View | ... | @@ -263,9 +276,8 @@ class Rivets.View |
263 | 276 | ||
264 | # Updates the view's models along with any affected bindings. | 277 | # Updates the view's models along with any affected bindings. |
265 | update: (models = {}) => | 278 | update: (models = {}) => |
266 | for key, model of models | 279 | @models[key] = model for key, model of models |
267 | @models[key] = model | 280 | binding.update models for binding in @bindings |
268 | binding.update() for binding in @select (b) -> b.key is key | ||
269 | 281 | ||
270 | # Rivets.TextTemplateParser | 282 | # Rivets.TextTemplateParser |
271 | # ------------------------- | 283 | # ------------------------- |
... | @@ -461,6 +473,9 @@ Rivets.binders = | ... | @@ -461,6 +473,9 @@ Rivets.binders = |
461 | @nested.unbind() | 473 | @nested.unbind() |
462 | delete @nested | 474 | delete @nested |
463 | 475 | ||
476 | update: (models) -> | ||
477 | @nested.update models | ||
478 | |||
464 | unless: | 479 | unless: |
465 | block: true | 480 | block: true |
466 | 481 | ||
... | @@ -473,6 +488,9 @@ Rivets.binders = | ... | @@ -473,6 +488,9 @@ Rivets.binders = |
473 | routine: (el, value) -> | 488 | routine: (el, value) -> |
474 | Rivets.binders.if.routine.call @, el, not value | 489 | Rivets.binders.if.routine.call @, el, not value |
475 | 490 | ||
491 | update: (models) -> | ||
492 | Rivets.binders.if.update.call @, models | ||
493 | |||
476 | "on-*": | 494 | "on-*": |
477 | function: true | 495 | function: true |
478 | 496 | ||
... | @@ -539,6 +557,14 @@ Rivets.binders = | ... | @@ -539,6 +557,14 @@ Rivets.binders = |
539 | else if @iterated[index].models[modelName] isnt model | 557 | else if @iterated[index].models[modelName] isnt model |
540 | @iterated[index].update data | 558 | @iterated[index].update data |
541 | 559 | ||
560 | update: (models) -> | ||
561 | data = {} | ||
562 | |||
563 | for key, model of models | ||
564 | data[key] = model unless key is @args[0] | ||
565 | |||
566 | view.update data for view in @iterated | ||
567 | |||
542 | "class-*": (el, value) -> | 568 | "class-*": (el, value) -> |
543 | elClass = " #{el.className} " | 569 | elClass = " #{el.className} " |
544 | 570 | ... | ... |
-
Please register or sign in to post a comment