forked from gmac/backbone.epoxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone.epoxy.min.js
6 lines (6 loc) · 11.2 KB
/
backbone.epoxy.min.js
1
2
3
4
5
6
// Backbone.Epoxy 1.3.3
// (c) 2015 Greg MacWilliam
// Freely distributed under the MIT license
// http://epoxyjs.org
(function(t,e){"undefined"!=typeof exports?module.exports=e(require("underscore"),require("backbone")):"function"==typeof define&&define.amd?define(["underscore","backbone"],e):e(t._,t.Backbone)})(this,function(t,e){function n(t,e,n){return t._super.prototype[e].apply(t,n)}function i(e,n,r,o){for(var s in n)if(n.hasOwnProperty(s)){var u=n[s];if(e.hasComputed(s)){if(o.length&&t.contains(o,s))throw"Recursive setter: "+o.join(" > ");u=e.c()[s].set(u),u&&_(u)&&(r=i(e,u,r,o.concat(s)))}else r[s]=u}return r}function r(e,n,i,r){i=i||{},i.get&&b(i.get)&&(i._get=i.get),i.set&&b(i.set)&&(i._set=i.set),delete i.get,delete i.set,t.extend(this,i),this.model=e,this.name=n,this.deps=this.deps||[],r||this.init()}function o(e){return b(e)?e():(_(e)&&(e=t.clone(e),t.each(e,function(t,n){e[n]=o(t)})),e)}function s(t){return b(t)?{set:t}:t}function u(e){return function(){var n=arguments,i=b(e)?e:e.get,r=e.set;return function(e){return w(e)?i.apply(this,t.map(n,o)):n[0]((r?r:i).call(this,e))}}}function c(e,n,i,r,o){return(e=t.result(e,r))?($(e)?(o=o?o+"_":"",n["$"+r]=function(){return F&&F.push([e,"change"]),e},t.each(e.toJSON({computed:!0}),function(t,r){n[o+r]=function(t){return a(e,r,t,i)}})):C(e)&&(n["$"+r]=function(){return F&&F.push([e,"reset add remove sort update"]),e}),e):void 0}function a(e,n,i,r){if(F&&F.push([e,"change:"+n]),!w(i)){if(!_(i)||x(i)||t.isDate(i)){var o=i;i={},i[n]=o}return r&&r.save?e.save(i,r):e.set(i,r)}return e.get(n)}function h(t,e){if(":el"===e||":scope"===e)return t.$el;var n=t.$(e);return t.$el.is(e)&&(n=n.add(t.$el)),n}function l(e,n,i,r,o,s){try{var u=B[i]||(B[i]=Function("$f","$c","with($f){with($c){return{"+i+"}}}")),c=u(s,r)}catch(a){throw'Error parsing bindings: "'+i+'"\n>> '+a}var h=t.map(t.union(c.events||[],["change"]),function(t){return t+".epoxy"}).join(" ");t.each(c,function(t,i){if(o.hasOwnProperty(i))e.b().push(new g(e,n,o[i],t,h,r,c));else if(!q.hasOwnProperty(i))throw'binding handler "'+i+'" is not defined.'})}function d(t,e,n){return t&&t.hasOwnProperty(e)?w(n)?o(t[e]):t[e](n):void 0}function f(t,e){var n=[];if(e&&t)for(var i=0,r=e.length;r>i;i++)n.push(e[i]in t?t[e[i]]():null);return n}function p(t){var e=[];for(var n in t){var i=t[n];_(i)&&(i="{"+p(i)+"}"),S.test(n)||N.test(n)||(n='"'+n+'"'),e.push(n+":"+i)}return e.join(",")}function g(e,n,i,r,s,u,c){var a=this,h=n[0].tagName.toLowerCase(),l="input"==h||"select"==h||"textarea"==h||"true"==n.prop("contenteditable"),d=[],f=function(t){a.$el&&a.set(a.$el,o(r),t)};if(a.view=e,a.$el=n,a.evt=s,t.extend(a,i),r=a.init(a.$el,o(r),u,c)||r,F=d,f(),F=null,l&&i.get&&b(r)&&a.$el.on(s,function(t){r(a.get(a.$el,o(r),t))}),d.length)for(var p=0,g=d.length;g>p;p++)a.listenTo(d[p][0],d[p][1],f)}var v,m=e.Epoxy={},y=Array.prototype,w=t.isUndefined,b=t.isFunction,_=t.isObject,x=t.isArray,$=function(t){return t instanceof e.Model},C=function(t){return t instanceof e.Collection},O=function(){},V={mixin:function(t){t=t||{};for(var e in this.prototype)"bindings"===e&&t.bindings||this.prototype.hasOwnProperty(e)&&"constructor"!==e&&(t[e]=this.prototype[e]);return t}},k=["computeds"];m.Model=e.Model.extend({_super:e.Model,constructor:function(e,i){t.extend(this,t.pick(i||{},k)),n(this,"constructor",arguments),this.initComputeds(this.attributes,i)},getCopy:function(e){return t.clone(this.get(e))},get:function(t){return v&&v.push(["change:"+t,this]),this.hasComputed(t)?this.c()[t].get():n(this,"get",arguments)},set:function(e,r,o){var s=e;s&&!_(s)?(s={},s[e]=r):o=r,o=o||{};var u=this._setting=[];o.unset||(s=i(this,s,{},[])),delete this._setting;var c=n(this,"set",[s,o]);return o.silent||(!this.hasChanged()&&u.length&&this.trigger("change",this),t.each(u,function(t){this.trigger.apply(this,t)},this)),c},toJSON:function(e){var i=n(this,"toJSON",arguments);return e&&e.computed&&t.each(this.c(),function(t,e){i[e]=t.value}),i},destroy:function(){return this.clearComputeds(),n(this,"destroy",arguments)},c:function(){return this._c||(this._c={})},initComputeds:function(e){this.clearComputeds();var n=t.result(this,"computeds")||{};n=t.extend(n,t.pick(e||{},t.keys(n))),t.each(n,function(t,e){t._init=1,this.addComputed(e,t)},this),t.invoke(this.c(),"init")},addComputed:function(t,e,n){this.removeComputed(t);var i=e,o=i._init;if(b(e)){var s=2;i={},i._get=e,b(n)&&(i._set=n,s++),i.deps=y.slice.call(arguments,s)}return this.c()[t]=new r(this,t,i,o),this},hasComputed:function(t){return this.c().hasOwnProperty(t)},removeComputed:function(t){return this.hasComputed(t)&&(this.c()[t].dispose(),delete this.c()[t]),this},clearComputeds:function(){for(var t in this.c())this.removeComputed(t);return this},modifyArray:function(t,e,n){var i=this.get(t);if(x(i)&&b(y[e])){var r=y.slice.call(arguments,2),o=y[e].apply(i,r);return n=n||{},n.silent||this.trigger("change:"+t+" change",this,y,n),o}return null},modifyObject:function(t,e,n,i){var r=this.get(t),o=!1;return _(r)?(i=i||{},w(n)&&r.hasOwnProperty(e)?(delete r[e],o=!0):r[e]!==n&&(r[e]=n,o=!0),o&&!i.silent&&this.trigger("change:"+t+" change",this,r,i),r):null}},V),t.extend(r.prototype,e.Events,{init:function(){var e={},n=v=[];this.get(!0),v=null,n.length&&(t.each(n,function(n){var i=n[0],r=n[1];e[i]?t.contains(e[i],r)||e[i].push(r):e[i]=[r]}),t.each(e,function(e,n){for(var i=0,r=e.length;r>i;i++)this.listenTo(e[i],n,t.bind(this.get,this,!0))},this))},val:function(t){return this.model.get(t)},get:function(e){if(e===!0&&this._get){var n=this._get.apply(this.model,t.map(this.deps,this.val,this));this.change(n)}return this.value},set:function(t){if(this._get){if(this._set)return this._set.apply(this.model,arguments);throw"Cannot set read-only computed attribute."}return this.change(t),null},change:function(e){if(!t.isEqual(e,this.value)){this.value=e;var n=["change:"+this.name,this.model,e];this.model._setting?this.model._setting.push(n):(n[0]+=" change",this.model.trigger.apply(this.model,n))}},dispose:function(){this.stopListening(),this.off(),this.model=this.value=null}});var P={optionText:"label",optionValue:"value"},B={},E={attr:s(function(t,e){t.attr(e)}),checked:s({get:function(e,n,i){e.length>1&&(e=e.filter(i.target));var r=!!e.prop("checked"),o=e.val();if(this.isRadio(e))return o;if(x(n)){n=n.slice();var s=t.indexOf(n,o);return r&&0>s?n.push(o):!r&&s>-1&&n.splice(s,1),n}return r},set:function(e,n){e.length>1&&(e=e.filter('[value="'+n+'"]'));var i=!!n;this.isRadio(e)?i=n==e.val():x(n)&&(i=t.contains(n,e.val())),e.prop("checked",i)},isRadio:function(t){return"radio"===t.attr("type").toLowerCase()}}),classes:s(function(e,n){t.each(n,function(t,n){e.toggleClass(n,!!t)})}),collection:s({init:function(t,e,n,i){if(this.i=i.itemView?this.view[i.itemView]:this.view.itemView,!C(e))throw'Binding "collection" requires a Collection.';if(!b(this.i))throw'Binding "collection" requires an itemView.';this.v={}},set:function(e,n,i){var r,o=this.v,s=this.i,u=n.models,c=F;if(F=null,i=i||n,$(i))if(o.hasOwnProperty(i.cid))o[i.cid].remove(),delete o[i.cid];else{o[i.cid]=r=new s({model:i,collectionView:this.view});var a=t.indexOf(u,i),h=e.children();h.length>a?h.eq(a).before(r.$el):e.append(r.$el)}else if(C(i)){var l=u.length===t.size(o)&&n.every(function(t){return o.hasOwnProperty(t.cid)});e.children().detach();var d=document.createDocumentFragment();l?n.each(function(t){d.appendChild(o[t.cid].el)}):(this.clean(),n.each(function(t){o[t.cid]=r=new s({model:t,collectionView:this.view}),d.appendChild(r.el)},this)),e.append(d)}F=c},clean:function(){for(var t in this.v)this.v.hasOwnProperty(t)&&(this.v[t].remove(),delete this.v[t])}}),css:s(function(t,e){t.css(e)}),disabled:s(function(t,e){t.prop("disabled",!!e)}),enabled:s(function(t,e){t.prop("disabled",!e)}),html:s(function(t,e){t.html(e)}),options:s({init:function(t,e,n,i){this.e=i.optionsEmpty,this.d=i.optionsDefault,this.v=i.value},set:function(e,n){var i=this,r=o(i.e),s=o(i.d),u=o(i.v),c=C(n)?n.models:n,a=c.length,h=!0,l="";a||s||!r?(s&&(c=[s].concat(c)),t.each(c,function(t){l+=i.opt(t,a)})):(l+=i.opt(r,a),h=!1),e.html(l).prop("disabled",!h).val(u),0>e[0].selectedIndex&&e.children().length&&(e[0].selectedIndex=0);var d=e.val();i.v&&!t.isEqual(u,d)&&i.v(d)},opt:function(t){var e=t,n=t,i=P.optionText,r=P.optionValue;return _(t)&&(e=$(t)?t.get(i):t[i],n=$(t)?t.get(r):t[r]),['<option value="',n,'">',e,"</option>"].join("")},clean:function(){this.d=this.e=this.v=0}}),template:s({init:function(e,n,i){var r=e.find("script,template");return this.t=t.template(r.length?r.html():e.html()),x(n)?t.pick(i,n):void 0},set:function(t,e){e=$(e)?e.toJSON({computed:!0}):e,t.html(this.t(e))},clean:function(){this.t=null}}),text:s({get:function(t){return t.text()},set:function(t,e){t.text(e)}}),toggle:s(function(t,e){t.toggle(!!e)}),value:s({get:function(t){return t.val()},set:function(t,e){try{t.val()+""!=e+""&&t.val(e)}catch(n){}}})},j={all:u(function(){for(var t=arguments,e=0,n=t.length;n>e;e++)if(!t[e])return!1;return!0}),any:u(function(){for(var t=arguments,e=0,n=t.length;n>e;e++)if(t[e])return!0;return!1}),length:u(function(t){return t.length||0}),none:u(function(){for(var t=arguments,e=0,n=t.length;n>e;e++)if(t[e])return!1;return!0}),not:u(function(t){return!t}),format:u(function(t){for(var e=arguments,n=1,i=e.length;i>n;n++)t=t.replace(RegExp("\\$"+n,"g"),e[n]);return t}),select:u(function(t,e,n){return t?e:n}),csv:u({get:function(t){return t+="",t?t.split(","):[]},set:function(t){return x(t)?t.join(","):t}}),integer:u(function(t){return t?parseInt(t,10):0}),decimal:u(function(t){return t?parseFloat(t):0})},q={events:1,itemView:1,optionsDefault:1,optionsEmpty:1};m.binding={allowedParams:q,addHandler:function(t,e){E[t]=s(e)},addFilter:function(t,e){j[t]=u(e)},config:function(e){t.extend(P,e)},emptyCache:function(){B={}}};var F,M=["viewModel","bindings","bindingFilters","bindingHandlers","bindingSources","computeds"];m.View=e.View.extend({_super:e.View,constructor:function(e){t.extend(this,t.pick(e||{},M)),n(this,"constructor",arguments),this.applyBindings()},b:function(){return this._b||(this._b=[])},bindings:"data-bind",setterOptions:null,applyBindings:function(){this.removeBindings();var n=this,i=t.clone(t.result(n,"bindingSources")),r=n.bindings,o=n.setterOptions,a=t.clone(E),d=t.clone(j),g=n._c={};t.each(t.result(n,"bindingHandlers")||{},function(t,e){a[e]=s(t)}),t.each(t.result(n,"bindingFilters")||{},function(t,e){d[e]=u(t)}),n.model=c(n,g,o,"model"),n.viewModel=c(n,g,o,"viewModel"),n.collection=c(n,g,o,"collection"),n.collection&&n.collection.view&&(n.itemView=n.collection.view),i&&(t.each(i,function(t,e){i[e]=c(i,g,o,e,e)}),n.bindingSources=i),t.each(t.result(n,"computeds")||{},function(t,e){var i=b(t)?t:t.get,r=t.set,o=t.deps;g[e]=function(t){return!w(t)&&r?r.call(n,t):i.apply(n,f(n._c,o))}}),_(r)?t.each(r,function(t,e){var i=h(n,e);_(t)&&(t=p(t)),i.length&&l(n,i,t,g,a,d)}):h(n,"["+r+"]").each(function(){var t=e.$(this);l(n,t,t.attr(r),g,a,d)})},getBinding:function(t){return d(this._c,t)},setBinding:function(t,e){return d(this._c,t,e)},removeBindings:function(){if(this._c=null,this._b)for(;this._b.length;)this._b.pop().dispose()},remove:function(){this.removeBindings(),n(this,"remove",arguments)}},V);var S=/^[a-z_$][a-z0-9_$]*$/i,N=/^\s*(["']).*\1\s*$/;return t.extend(g.prototype,e.Events,{init:O,get:O,set:O,clean:O,dispose:function(){this.clean(),this.stopListening(),this.$el.off(this.evt),this.$el=this.view=null}}),m});
//@ sourceMappingURL=backbone.epoxy.min.map