Build 0.5.4.
Showing
5 changed files
with
59 additions
and
43 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.3", | 5 | "version": "0.5.4", |
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.3 | 2 | // version: 0.5.4 |
3 | // author: Michael Richards | 3 | // author: Michael Richards |
4 | // license: MIT | 4 | // license: MIT |
5 | (function() { | 5 | (function() { |
... | @@ -32,6 +32,7 @@ | ... | @@ -32,6 +32,7 @@ |
32 | this.publish = __bind(this.publish, this); | 32 | this.publish = __bind(this.publish, this); |
33 | this.sync = __bind(this.sync, this); | 33 | this.sync = __bind(this.sync, this); |
34 | this.set = __bind(this.set, this); | 34 | this.set = __bind(this.set, this); |
35 | this.eventHandler = __bind(this.eventHandler, this); | ||
35 | this.formattedValue = __bind(this.formattedValue, this); | 36 | this.formattedValue = __bind(this.formattedValue, this); |
36 | if (!(this.binder = this.view.binders[type])) { | 37 | if (!(this.binder = this.view.binders[type])) { |
37 | _ref = this.view.binders; | 38 | _ref = this.view.binders; |
... | @@ -54,7 +55,7 @@ | ... | @@ -54,7 +55,7 @@ |
54 | }; | 55 | }; |
55 | } | 56 | } |
56 | this.formatters = this.options.formatters || []; | 57 | this.formatters = this.options.formatters || []; |
57 | this.model = this.view.models[this.key]; | 58 | this.model = this.key ? this.view.models[this.key] : this.view.models; |
58 | } | 59 | } |
59 | 60 | ||
60 | Binding.prototype.formattedValue = function(value) { | 61 | Binding.prototype.formattedValue = function(value) { |
... | @@ -75,6 +76,15 @@ | ... | @@ -75,6 +76,15 @@ |
75 | return value; | 76 | return value; |
76 | }; | 77 | }; |
77 | 78 | ||
79 | Binding.prototype.eventHandler = function(fn) { | ||
80 | var binding, handler; | ||
81 | |||
82 | handler = (binding = this).view.config.handler; | ||
83 | return function(ev) { | ||
84 | return handler.call(fn, this, ev, binding); | ||
85 | }; | ||
86 | }; | ||
87 | |||
78 | Binding.prototype.set = function(value) { | 88 | Binding.prototype.set = function(value) { |
79 | var _ref; | 89 | var _ref; |
80 | 90 | ||
... | @@ -165,7 +175,7 @@ | ... | @@ -165,7 +175,7 @@ |
165 | 175 | ||
166 | Binding.prototype.update = function() { | 176 | Binding.prototype.update = function() { |
167 | this.unbind(); | 177 | this.unbind(); |
168 | this.model = this.view.models[this.key]; | 178 | this.model = this.key ? this.view.models[this.key] : this.view.models; |
169 | return this.bind(); | 179 | return this.bind(); |
170 | }; | 180 | }; |
171 | 181 | ||
... | @@ -302,7 +312,7 @@ | ... | @@ -302,7 +312,7 @@ |
302 | splitPath.shift(); | 312 | splitPath.shift(); |
303 | } | 313 | } |
304 | keypath = splitPath.join('.'); | 314 | keypath = splitPath.join('.'); |
305 | if (_this.models[key] != null) { | 315 | if (!key || (_this.models[key] != null)) { |
306 | if (dependencies = context.shift()) { | 316 | if (dependencies = context.shift()) { |
307 | options.dependencies = dependencies.split(/\s+/); | 317 | options.dependencies = dependencies.split(/\s+/); |
308 | } | 318 | } |
... | @@ -425,40 +435,34 @@ | ... | @@ -425,40 +435,34 @@ |
425 | })(); | 435 | })(); |
426 | 436 | ||
427 | Rivets.Util = { | 437 | Rivets.Util = { |
428 | bindEvent: function(el, event, handler, view) { | 438 | bindEvent: function(el, event, handler) { |
429 | var fn; | ||
430 | |||
431 | fn = function(ev) { | ||
432 | return handler.call(this, ev, view); | ||
433 | }; | ||
434 | if (window.jQuery != null) { | 439 | if (window.jQuery != null) { |
435 | el = jQuery(el); | 440 | el = jQuery(el); |
436 | if (el.on != null) { | 441 | if (el.on != null) { |
437 | el.on(event, fn); | 442 | return el.on(event, handler); |
438 | } else { | 443 | } else { |
439 | el.bind(event, fn); | 444 | return el.bind(event, handler); |
440 | } | 445 | } |
441 | } else if (window.addEventListener != null) { | 446 | } else if (window.addEventListener != null) { |
442 | el.addEventListener(event, fn, false); | 447 | return el.addEventListener(event, handler, false); |
443 | } else { | 448 | } else { |
444 | event = 'on' + event; | 449 | event = 'on' + event; |
445 | el.attachEvent(event, fn); | 450 | return el.attachEvent(event, handler); |
446 | } | 451 | } |
447 | return fn; | ||
448 | }, | 452 | }, |
449 | unbindEvent: function(el, event, fn) { | 453 | unbindEvent: function(el, event, handler) { |
450 | if (window.jQuery != null) { | 454 | if (window.jQuery != null) { |
451 | el = jQuery(el); | 455 | el = jQuery(el); |
452 | if (el.off != null) { | 456 | if (el.off != null) { |
453 | return el.off(event, fn); | 457 | return el.off(event, handler); |
454 | } else { | 458 | } else { |
455 | return el.unbind(event, fn); | 459 | return el.unbind(event, handler); |
456 | } | 460 | } |
457 | } else if (window.removeEventListener) { | 461 | } else if (window.removeEventListener != null) { |
458 | return el.removeEventListener(event, fn, false); | 462 | return el.removeEventListener(event, handler, false); |
459 | } else { | 463 | } else { |
460 | event = 'on' + event; | 464 | event = 'on' + event; |
461 | return el.detachEvent(event, fn); | 465 | return el.detachEvent(event, handler); |
462 | } | 466 | } |
463 | }, | 467 | }, |
464 | getInputValue: function(el) { | 468 | getInputValue: function(el) { |
... | @@ -503,10 +507,10 @@ | ... | @@ -503,10 +507,10 @@ |
503 | checked: { | 507 | checked: { |
504 | publishes: true, | 508 | publishes: true, |
505 | bind: function(el) { | 509 | bind: function(el) { |
506 | return this.currentListener = Rivets.Util.bindEvent(el, 'change', this.publish); | 510 | return Rivets.Util.bindEvent(el, 'change', this.publish); |
507 | }, | 511 | }, |
508 | unbind: function(el) { | 512 | unbind: function(el) { |
509 | return Rivets.Util.unbindEvent(el, 'change', this.currentListener); | 513 | return Rivets.Util.unbindEvent(el, 'change', this.publish); |
510 | }, | 514 | }, |
511 | routine: function(el, value) { | 515 | routine: function(el, value) { |
512 | var _ref; | 516 | var _ref; |
... | @@ -521,10 +525,10 @@ | ... | @@ -521,10 +525,10 @@ |
521 | unchecked: { | 525 | unchecked: { |
522 | publishes: true, | 526 | publishes: true, |
523 | bind: function(el) { | 527 | bind: function(el) { |
524 | return this.currentListener = Rivets.Util.bindEvent(el, 'change', this.publish); | 528 | return Rivets.Util.bindEvent(el, 'change', this.publish); |
525 | }, | 529 | }, |
526 | unbind: function(el) { | 530 | unbind: function(el) { |
527 | return Rivets.Util.unbindEvent(el, 'change', this.currentListener); | 531 | return Rivets.Util.unbindEvent(el, 'change', this.publish); |
528 | }, | 532 | }, |
529 | routine: function(el, value) { | 533 | routine: function(el, value) { |
530 | var _ref; | 534 | var _ref; |
... | @@ -548,10 +552,10 @@ | ... | @@ -548,10 +552,10 @@ |
548 | value: { | 552 | value: { |
549 | publishes: true, | 553 | publishes: true, |
550 | bind: function(el) { | 554 | bind: function(el) { |
551 | return this.currentListener = Rivets.Util.bindEvent(el, 'change', this.publish); | 555 | return Rivets.Util.bindEvent(el, 'change', this.publish); |
552 | }, | 556 | }, |
553 | unbind: function(el) { | 557 | unbind: function(el) { |
554 | return Rivets.Util.unbindEvent(el, 'change', this.currentListener); | 558 | return Rivets.Util.unbindEvent(el, 'change', this.publish); |
555 | }, | 559 | }, |
556 | routine: function(el, value) { | 560 | routine: function(el, value) { |
557 | var o, _i, _len, _ref, _ref1, _ref2, _results; | 561 | var o, _i, _len, _ref, _ref1, _ref2, _results; |
... | @@ -586,11 +590,16 @@ | ... | @@ -586,11 +590,16 @@ |
586 | }, | 590 | }, |
587 | "on-*": { | 591 | "on-*": { |
588 | "function": true, | 592 | "function": true, |
593 | unbind: function(el) { | ||
594 | if (this.handler) { | ||
595 | return Rivets.Util.unbindEvent(el, this.args[0], this.handler); | ||
596 | } | ||
597 | }, | ||
589 | routine: function(el, value) { | 598 | routine: function(el, value) { |
590 | if (this.currentListener) { | 599 | if (this.handler) { |
591 | Rivets.Util.unbindEvent(el, this.args[0], this.currentListener); | 600 | Rivets.Util.unbindEvent(el, this.args[0], this.handler); |
592 | } | 601 | } |
593 | return this.currentListener = Rivets.Util.bindEvent(el, this.args[0], value, this.view); | 602 | return Rivets.Util.bindEvent(el, this.args[0], this.handler = this.eventHandler(value)); |
594 | } | 603 | } |
595 | }, | 604 | }, |
596 | "each-*": { | 605 | "each-*": { |
... | @@ -598,12 +607,14 @@ | ... | @@ -598,12 +607,14 @@ |
598 | bind: function(el) { | 607 | bind: function(el) { |
599 | var attr; | 608 | var attr; |
600 | 609 | ||
601 | attr = ['data', this.view.config.prefix, this.type].join('-').replace('--', '-'); | 610 | if (this.marker == null) { |
602 | this.marker = document.createComment(" rivets: " + this.type + " "); | 611 | attr = ['data', this.view.config.prefix, this.type].join('-').replace('--', '-'); |
603 | this.iterated = []; | 612 | this.marker = document.createComment(" rivets: " + this.type + " "); |
604 | el.removeAttribute(attr); | 613 | this.iterated = []; |
605 | el.parentNode.insertBefore(this.marker, el); | 614 | el.removeAttribute(attr); |
606 | return el.parentNode.removeChild(el); | 615 | el.parentNode.insertBefore(this.marker, el); |
616 | return el.parentNode.removeChild(el); | ||
617 | } | ||
607 | }, | 618 | }, |
608 | unbind: function(el) { | 619 | unbind: function(el) { |
609 | var view, _i, _len, _ref, _results; | 620 | var view, _i, _len, _ref, _results; |
... | @@ -656,7 +667,9 @@ | ... | @@ -656,7 +667,9 @@ |
656 | } | 667 | } |
657 | options.config.preloadData = true; | 668 | options.config.preloadData = true; |
658 | template = el.cloneNode(true); | 669 | template = el.cloneNode(true); |
659 | this.iterated.push(rivets.bind(template, data, options)); | 670 | view = new Rivets.View(template, data, options); |
671 | view.bind(); | ||
672 | this.iterated.push(view); | ||
660 | _results.push(this.marker.parentNode.insertBefore(template, previous.nextSibling)); | 673 | _results.push(this.marker.parentNode.insertBefore(template, previous.nextSibling)); |
661 | } else if (this.iterated[index].models[modelName] !== model) { | 674 | } else if (this.iterated[index].models[modelName] !== model) { |
662 | _results.push(this.iterated[index].update(data)); | 675 | _results.push(this.iterated[index].update(data)); |
... | @@ -685,7 +698,10 @@ | ... | @@ -685,7 +698,10 @@ |
685 | }; | 698 | }; |
686 | 699 | ||
687 | Rivets.config = { | 700 | Rivets.config = { |
688 | preloadData: true | 701 | preloadData: true, |
702 | handler: function(context, ev, binding) { | ||
703 | return this.call(context, ev, binding.view.models); | ||
704 | } | ||
689 | }; | 705 | }; |
690 | 706 | ||
691 | Rivets.formatters = {}; | 707 | Rivets.formatters = {}; | ... | ... |
1 | // Rivets.js | 1 | // Rivets.js |
2 | // version: 0.5.3 | 2 | // version: 0.5.4 |
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,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.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.view.models[this.key]}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.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(){return this.unbind(),this.model=this.view.models[this.key],this.bind()},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,x,E,k,j,L,N,U,V,Q,R,B,A,F;if(0>n.call(h,e)){for(R=e.attributes,j=0,U=R.length;U>j;j++)if(s=R[j],i.test(s.name)){if(E=s.name.replace(i,""),!(o=f.binders[E])){B=f.binders;for(d in B)k=B[d],"*"!==d&&-1!==d.indexOf("*")&&(w=RegExp("^"+d.replace("*",".+")+"$"),w.test(E)&&(o=k))}if(o||(o=f.binders["*"]),o.block){for(A=e.getElementsByTagName("*"),L=0,V=A.length;V>L;L++)b=A[L],h.push(b);r=[s]}}for(F=r||e.attributes,N=0,Q=F.length;Q>N;N++)s=F[N],i.test(s.name)&&(v={},E=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(),x=g.split(/\.|:/),v.formatters=m,v.bypass=-1!==g.indexOf(":"),x[0]?c=x.shift():(c=null,x.shift()),p=x.join("."),null!=f.models[c]&&((a=u.shift())&&(v.dependencies=a.split(/\s+/)),f.bindings.push(new t.Binding(f,e,E,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;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,n){var s;return s=function(t){return e.call(this,t,n)},null!=window.jQuery?(t=jQuery(t),null!=t.on?t.on(i,s):t.bind(i,s)):null!=window.addEventListener?t.addEventListener(i,s,!1):(i="on"+i,t.attachEvent(i,s)),s},unbindEvent:function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.off?t.off(i,e):t.unbind(i,e)):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 this.currentListener=t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.currentListener)},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 this.currentListener=t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.currentListener)},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 this.currentListener=t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.currentListener)},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:""},"on-*":{"function":!0,routine:function(i,e){return this.currentListener&&t.Util.unbindEvent(i,this.args[0],this.currentListener),this.currentListener=t.Util.bindEvent(i,this.args[0],e,this.view)}},"each-*":{block:!0,bind:function(t){var i;return 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)},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(t,i){var e,n,s,r,h,o,u,l,a,d,c,f,p,b,v,g,y,m,w,x;if(u=this.args[0],i=i||[],this.iterated.length>i.length)for(y=Array(this.iterated.length-i.length),p=0,v=y.length;v>p;p++)n=y[p],f=this.iterated.pop(),f.unbind(),this.marker.parentNode.removeChild(f.els[0]);for(x=[],s=b=0,g=i.length;g>b;s=++b)if(o=i[s],e={},e[u]=o,null==this.iterated[s]){m=this.view.models;for(h in m)o=m[h],e[h]=o;a=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,l={binders:this.view.options.binders,formatters:this.view.options.formatters,config:{}},w=this.view.options.config;for(r in w)c=w[r],l.config[r]=c;l.config.preloadData=!0,d=t.cloneNode(!0),this.iterated.push(rivets.bind(d,e,l)),x.push(this.marker.parentNode.insertBefore(d,a.nextSibling))}else this.iterated[s].models[u]!==o?x.push(this.iterated[s].update(e)):x.push(void 0);return x}},"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},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,l,u,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)u=a[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,a;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=(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,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,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"],u=0,a=d.length;a>u;u++){if(h=d[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,a,d,c,f=this;for(this.bindings=[],h=[],i=this.bindingRegExp(),r=function(e){var s,r,o,l,u,a,d,c,p,b,v,g,y,m,w,k,E,x,j,U,N,V,Q,R,B,A,F,H;if(0>n.call(h,e)){for(B=e.attributes,j=0,V=B.length;V>j;j++)if(s=B[j],i.test(s.name)){if(E=s.name.replace(i,""),!(o=f.binders[E])){A=f.binders;for(d in A)x=A[d],"*"!==d&&-1!==d.indexOf("*")&&(w=RegExp("^"+d.replace("*",".+")+"$"),w.test(E)&&(o=x))}if(o||(o=f.binders["*"]),o.block){for(F=e.getElementsByTagName("*"),U=0,Q=F.length;Q>U;U++)b=F[U],h.push(b);r=[s]}}for(H=r||e.attributes,N=0,R=H.length;R>N;N++)s=H[N],i.test(s.name)&&(v={},E=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}(),l=function(){var t,i,e,n;for(e=m.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=m,v.bypass=-1!==g.indexOf(":"),k[0]?c=k.shift():(c=null,k.shift()),p=k.join("."),c&&null==f.models[c]||((a=l.shift())&&(v.dependencies=a.split(/\s+/)),f.bindings.push(new t.Binding(f,e,E,c,p,v))));r&&(r=null)}},d=this.els,o=0,u=d.length;u>o;o++)for(e=d[o],r(e),c=e.getElementsByTagName("*"),l=0,a=c.length;a>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:""},"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,a,d,c,f,p,b,v,g,y,m,w,k,E;if(u=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(l=e[r],n={},n[u]=l,null==this.iterated[r]){w=this.view.models;for(o in w)l=w[o],n[o]=l;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:{}},k=this.view.options.config;for(h in k)f=k[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[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 | ... | ... |
1 | { | 1 | { |
2 | "name": "rivets", | 2 | "name": "rivets", |
3 | "description": "Declarative data binding facility.", | 3 | "description": "Declarative data binding facility.", |
4 | "version": "0.5.3", | 4 | "version": "0.5.4", |
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", | ... | ... |
-
Please register or sign in to post a comment