-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackbone-reaction.min.js
4 lines (4 loc) · 16.6 KB
/
backbone-reaction.min.js
1
2
3
4
/*!
* https://github.com/jhudson8/backbone-reaction v0.12.0; MIT license; Joe Hudson<joehud_AT_gmail.com>
*/
!function(e){"function"==typeof define&&define.amd?define([],function(){return e}):"undefined"!=typeof exports&&"undefined"!=typeof require?module.exports=e:e(React,Backbone,_)}(function(React,Backbone,_){!function(){function e(e){e=e||"_all";var n=u[e];return n||(n=function(n,i){"_all"!==e&&(options=i,i=n,n=e),t(i.method,this,i.model,i.options)},u[e]=n),n}function t(e,t,r,d){function u(e){var a=d[e];d[e]=function(r,o,s){if(e!==c||v.preventDefault||(v.trigger("after-send",r,o,s,e,v),r=v.data||r,!v.preventDefault)){try{a&&a.call(this,r,o,s)}finally{var l=f.indexOf(v);l>=0&&f.splice(l,1),0===f.length&&(t[i]=void 0,t.trigger(n,v))}var d=e===c?[e,v]:[e,r,o,s,v];v.trigger.apply(v,d),d.splice(0,0,"complete"),v.trigger.apply(v,d)}}}var f=t[i]=t[i]||[],h=d&&d.event||e,v=new l(e,r,d),p=a+":"+h;t.trigger(a,h,v),t.trigger(p,v),t===r&&(o.trigger(a,h,t,v),o.trigger(p,t,v));var m=d.beforeSend;return d.beforeSend=function(e,t){if(v.xhr=e,v.settings=t,m){var n=m.call(this,e,t);if(n===!1)return n}return v.trigger("before-send",e,t,v),v.preventDefault?!1:void f.push(v)},u(c),u(s),v}var n=Backbone.xhrCompleteEventName=Backbone.xhrCompleteEventName||"xhr:complete",i=Backbone.xhrModelLoadingAttribute=Backbone.xhrModelLoadingAttribute||"xhrActivity",a=Backbone.xhrEventName=Backbone.xhrEventName||"xhr",r=Backbone.xhrGlobalAttribute=Backbone.xhrGlobalAttribute||"xhrEvents",o=Backbone[r]=_.extend({},Backbone.Events),c="success",s="error",l=function(e,t,n){this.method=e,this.model=t,this.options=n};l.prototype.abort=function(){this.aborted||(this.aborted=!0,this.preventDefault=!0,this.xhr&&this.xhr.abort())},_.extend(l.prototype,Backbone.Events);var d=Backbone.sync;Backbone.sync=function(e,n,i){i=i||{},i.url||(i.url=_.result(n,"url"));var a=t(e,n,n,i);if(!a.preventDefault){var r=d.call(this,e,n,i);return a.xhr=r,r}},o.on(a+":read",function(e,t){t.on(c,function(){e.hasBeenFetched=!0,e.hadFetchError=!1}),t.on(s,function(){e.hadFetchError=!0})}),Backbone.Model.prototype.whenFetched=Backbone.Collection.whenFetched=function(e,t){function n(){e(a)}var a=this;if(this.hasBeenFetched)return e(this);var r=_.find(this[i],function(e){return"read"===e.method});r?(r.on("success",n),t&&r.on("error",t)):this.fetch({success:n,error:t})},Backbone.forwardXhrEvents=function(t,n,i){var r=e(!_.isFunction(i)&&i);if(_.isFunction(i))try{t.on(a,r,n),i.call(this)}finally{t.off(a,r,n)}else{var o=i?a+":"+i:a;t.on(o,r,n)}},Backbone.stopXhrForwarding=function(t,n,i){var r=e(i);t.off(a,r,n)};var u={}}(),function(){function setState(e,t){if(t.isMounted())t.setState(e);else if(t.state)for(var n in e)t.state[n]=e[n];else{var i=t.__temporary_state||{};for(var n in e)i[n]=e[n];t.__temporary_state=i}}function getState(e,t){var n=t.state,i=t.__temporary_state;return n&&n[e]||i&&i[e]}function get(values,index,initiatedOnce,rtn){function addTo(name){var indexName=name,match=name.match(/^([^\(]*)\s*\(([^\)]*)\)\s*/),params=match&&match[2];if(name=match&&match[1]||name,!index[indexName]){params&&(params=eval("["+params+"]"));var mixin=React.mixins._mixins[name],checkAgain=!1,skip=!1;if(!mixin)throw new Error('invalid mixin "'+name+'"');if("function"==typeof mixin)React.mixins._initiatedOnce[name]?(initiatedOnce[name]=initiatedOnce[name]||[],initiatedOnce[name].push(params),skip=!0):(mixin=mixin.apply(this,params||[]),checkAgain=!0);else if(params)throw new Error('the mixin "'+name+'" does not support parameters');get(React.mixins._dependsOn[name],index,initiatedOnce,rtn),get(React.mixins._dependsInjected[name],index,initiatedOnce,rtn),index[indexName]=!0,checkAgain?get([mixin],index,initiatedOnce,rtn):skip||rtn.push(mixin)}}function handleMixin(e){e&&(Array.isArray(e)?get(e,index,initiatedOnce,rtn):"string"==typeof e?addTo(e):rtn.push(e))}if(Array.isArray(values))for(var i=0;i<values.length;i++)handleMixin(values[i]);else handleMixin(values)}function getInitiatedOnce(e,t){function n(e,n){e=e.apply(this,n||[]),t.push(e)}for(var i in e)e.hasOwnProperty(i)&&n(React.mixins._mixins[i],e[i])}function addMixin(e,t,n,i,a){var r=React.mixins;(i||!r._mixins[e])&&(r._dependsOn[e]=n.length&&n,r._mixins[e]=t,a&&(r._initiatedOnce[e]=!0))}function GROUP(){}function mixinParams(e,t){var n,i=e[0],a=!1;if("object"==typeof i?(n=i.name,a=i.initiatedOnce):n=i,!n||!n.length)throw new Error("the mixin name hasn't been specified");return Array.isArray(e[1])?[n,e[1][0],Array.prototype.slice.call(e[1],1),t,a]:[n,e[1],Array.prototype.slice.call(e,2),t,a]}var _createClass=React.createClass;React.createClass=function(e){return e.mixins&&(e.mixins=React.mixins.get(e.mixins)),_createClass.apply(React,arguments)},React.mixins={get:function(){var e=[],t={},n={};return get(Array.prototype.slice.call(arguments),t,n,e),getInitiatedOnce(n,e),e},inject:function(e){var t=this._dependsInjected[e];t||(t=this._dependsInjected[e]=[]),t.push(Array.prototype.slice.call(arguments,1))},alias:function(e){addMixin(e,GROUP,Array.prototype.slice.call(arguments,1),!1)},add:function(){addMixin.apply(this,mixinParams(arguments,!1))},replace:function(){addMixin.apply(this,mixinParams(arguments,!0))},exists:function(e){return this._mixins[e]||!1},_dependsOn:{},_dependsInjected:{},_mixins:{},_initiatedOnce:{}},React.mixins.add("deferUpdate",{getInitialState:function(){return{}},deferUpdate:function(){var e=this.state;if(!e._deferUpdate){e._deferUpdate=!0;var t=this;setTimeout(function(){delete e._deferUpdate,t.isMounted()&&t.forceUpdate()},0)}}}),React.mixins.add("state",{getInitialState:function(){return{}},componentWillMount:function(){var e=this.__temporary_state;if(e){for(var t in e)this.state[t]=e[t];delete this.__temporary_state}}}),React.mixins.setState=setState,React.mixins.getState=getState}(),function(){function e(t,n,i){n=n||{},i?i+=":":i="";var a,r;for(var o in t)a=t[o],r=typeof a,"string"===r||"function"===r?n[i+o]=a:a&&e(a,n,i+o);return n}function t(e,t){var n={type:e};for(var i in t)n[i]=t[i];var r=React.mixins.getState("__watchedEvents",this);r||(r=[],f({__watchedEvents:r},this)),n.context=n.context||this,r.push(n);var o=a(n.target,this);if(this.isMounted()&&o&&o[n.type](n.event,n.callback,n.context),"off"===e)for(var c,s=0;s<r.length;s++)c=r[s],c.event===t.event&&c.callback===t.callback&&a(c.target,this)===o&&r.splice(s,1)}function n(e){var t=h("__watchedEvents",e);if(t){var n;for(var i in t){n=t[i];var r=a(n.target,e);r&&r[n.type](n.event,n.callback,n.context)}}}function i(e,t){var n=h("__watchedEvents",t);if(n){var i;for(var r in n){i=n[r];var o=a(i.target,t);o&&o.off(i.event,i.callback,i.context)}e||f({__watchedEvents:[]},t)}}function a(e,t){return"function"==typeof e?e.call(t):e}function r(e,t,n,i){if(!i){var a,o=t;if("object"==typeof t&&(o=t.callback.call(this)),"string"==typeof t&&(a=u.indexOf(t)>=0,o=n[t]),!o)throw'no callback function exists for "'+t+'"';t=function(){return o.apply(n,a?[]:arguments)}}var f=e.match(d);if(f){var h=f[1],v=f[2].split(/\s*,\s*/),p=f[3],m=React.events.specials[h];if(m)return 1===v.length&&""===v[0]&&(v=[]),t=m.call(n,t,v),r(p,t,n,!0);throw new Error('invalid special event handler "'+h+"'")}var g=e.match(l),x=g[1];path=g[2],handler=c[x];for(var y=0;!handler&&y<s.length;y++)x.match(s[y].pattern)&&(handler=s[y].handler);if(!handler)throw'no handler registered for "'+e+'"';return handler.call(n,{key:x,path:path},t)}function o(e,t){p.handle(e,function(e,n){var i,a,r=e.path.match(l),o=r[1],c=r[2];return{on:function(){var e=t.call(this,o);e&&(a=e.state||e,e.on(c,n),i=e)},off:function(){i&&(i.off(c,n),i=void 0,a=void 0)},isStale:function(){if(!i)return!!e;var e=t.call(this,o);return e&&(e.state||e)===a?void 0:!0}}})}var c={},s=[],l=/^([^:]+):?(.*)/,d=/^\*([^\(]+)\(([^)]*)\):(.*)/,u=["forceUpdate"],f=React.mixins.setState,h=React.mixins.getState,v={standard:function(e){var t={on:e.onKey||"on",off:e.offKey||"off"},n=e.target;return function(i,a){function r(e,i){return function(){var r="function"==typeof n?n.call(i,o):n;r&&r[t[e]](o,a)}}var o=i.path;return{on:r("on",this),off:r("off",this),initialize:e.initialize}}}},p=React.events={specials:{},handle:function(e,t){"function"!=typeof t&&(t=v[t.type||"standard"](t)),e instanceof RegExp?s.push({pattern:e,handler:t}):c[e]=t}};"undefined"!=typeof window&&p.handle("window",{target:window,onKey:"addEventListener",offKey:"removeEventListener"});var m={ref:function(e){return this.refs[e]},prop:function(e){return this.props[e]}};for(var g in m)o(g,m[g]);p.handle("repeat",function(e,t){var n,i=parseInt(e.path,10);return{on:function(){n=setInterval(t,i)},off:function(){n=!!clearInterval(n)}}}),p.handle("!repeat",function(e,t){function n(e){e!==!0&&t(),setTimeout(function(){i&&requestAnimationFrame(n)},a)}var i,a=parseInt(e.path,10);return{on:function(){i=!0,n(!0)},off:function(){i=!1}}}),React.mixins.add("events",function(){function t(e,t){return function(){e.apply(t,arguments)}}var n=[{triggerWith:function(){var e=Array.prototype.slice.call(arguments),t=this;return function(){t.trigger.apply(this,e)}},getInitialState:function(){var t=[];if(this.events){var n,i=e(this.events);for(var a in i)n=r(a,i[a],this),n.initialize&&n.initialize.call(this),t.push(n)}return{_eventHandlers:t}},componentDidUpdate:function(){for(var e,t=h("_eventHandlers",this),n=0;n<t.length;n++)e=t[n],e.isStale&&e.isStale.call(this)&&(e.off.call(this),e.on.call(this))},componentDidMount:function(){for(var e=h("_eventHandlers",this),t=0;t<e.length;t++)e[t].on.call(this)},componentWillUnmount:function(){for(var e=h("_eventHandlers",this),t=0;t<e.length;t++)e[t].off.call(this)}}];if(p.mixin){var i={},a={};for(var o in p.mixin)i[o]=t(p.mixin[o],a);i.getInitialState=function(){return{__events:a}},n.push(i)}return n},"state"),React.mixins.add("listen",{componentDidMount:function(){i(!0,this),n(this)},componentWillUnmount:function(){i(!0,this)},listenTo:function(e,n,i,a){var r=n?{event:n,callback:i,target:e,context:a}:e;t.call(this,"on",r)},listenToOnce:function(e,n,i,a){var r={event:n,callback:i,target:e,context:a};t.call(this,"once",r)},stopListening:function(e,n,i,a){var r={event:n,callback:i,target:e,context:a};t.call(this,"off",r)}})}(),function(){function e(e){return e?_.isArray(e)?e:[e]:void 0}function t(e){return function(){return e?e:this.getModel?this.getModel():void 0}}function n(e){return e.getModelKey?e.getModelKey():e.props.key||e.props.ref||e.props.name}function i(e,t){if(e.getModel){var i=n(e),a=e.getModel();if(a)return t(i,a)}}function a(t,n,i,a){var r,o,c=Array.isArray(t)?t:e(n.props[t]);if(c){for(var s=0;s<c.length;s++)r=c[s],o=i.replace("{key}",r),n.modelOn(o,_.bind(a,n),this);return c}}function r(e,t,n,i){var a=o(n),r=t[0],c=t[1],s=t[2];a[r]={type:e,ev:r,cb:c,ctx:s};var l=i||n.getModel();l&&n["on"===e?"listenTo":"listenToOnce"](l,r,c,s)}function o(e){var t=u("__modelEvents",e);return t||(t={},f({__modelEvents:t},e)),t}function c(e){for(var t,n=0;n<e.length;n++)t=e[n],"true"===t?t=!0:"false"===t?t=!1:t.match(/^[0-9]+$/)?t=parseInt(t):t.match(/^[0-9]+\.[0-9]+/)&&(t=parseFloat(t)),e[n]=t;return e}var s=Backbone.xhrEventName,l=Backbone.xhrCompleteEventName,d=Backbone.xhrModelLoadingAttribute,u=React.mixins.getState,f=React.mixins.setState;Backbone.input=Backbone.input||{};{var h=Backbone.input.getModelValue=function(e){return i(e,function(e,t){return t.get(e)})};Backbone.input.setModelValue=function(e,t,n){return i(e,function(e,i){return i.set(e,t,n)})}}if(React.mixins.add("modelAware",{getModel:function(e){return e=e||this.props,u("model",this)||u("collection",this)||e.model||e.collection},setModel:function(e,t){var n=this.getModel(),i=o(this);_.each(i,function(t){this.modelOff(t.ev,t.cb,t.ctx,n),r(t.type,[t.ev,t.cb,t.ctx],this,e)},this),t!==!0&&f("model",e)}},"state"),React.mixins.add("modelPopulate",{modelPopulate:function(){var e,t,i,a;_.each(arguments,function(n){n instanceof Backbone.Model||n===!1?a=n:_.isArray(n)?e=n:_.isFunction(n)?t=n:i=n}),_.isUndefined(a)&&this.getModel&&(a=this.getModel());var r={};e||(e=_.map(this.refs,function(e){return e}));var o={};if(_.each(e,function(e){if(e.getValue){var t=n(e);if(t){var c=e.getValue();r[t]=c}}else if(e.modelPopulate&&e.getModel){if(!a)return;var s=e.getModel();if(s){var l=e.modelPopulate(i,!1),d=o[s.cid]||{};_.extend(d,l),o[s.cid]={model:s,attr:d}}}}),a){var c=!0,s=o[a.cid];s||(s={model:a,attr:{}}),_.extend(s.attr,r),o[a.cid]=s;var l=_.defaults({validate:!0},i);_.each(o,function(e){var t=!e.model._validate(e.attr,l);c=!t&&c}),c&&(i=_.defaults({validate:!1},i),_.each(o,function(e){e.model.set(e.attr,i)})),t&&c&&t.call(this,a)}return r}},"modelAware"),React.mixins.add("modelValidator",{modelValidate:function(e,t){var n=this.getModel();return n&&n.validate?this.modelIndexErrors(n.validate(e,t))||!1:void 0}},"modelAware","modelIndexErrors"),React.mixins.add("modelEventAware",{getInitialState:function(){var e=this.getModel();return e&&(e.off&&e.on||(console.error("the model does not implement on/off functions - you will see problems"),console.log(e))),{}},componentWillReceiveProps:function(e){var t=this.getModel(),n=this.getModel(e);t!==n&&this.setModel(n,!0)},modelOn:function(){r("on",arguments,this)},modelOnce:function(){r("once",arguments,this)},modelOff:function(e,n,i,a){var r=o(this);delete r[e],this.stopListening(t(a),e,n,i)}},"modelAware","listen"),React.mixins.add("modelChangeAware",{getInitialState:function(){return _.each(["change","reset","add","remove","sort"],function(e){this.modelOn(e,function(){this.deferUpdate()})},this),null}},"modelEventAware","deferUpdate"),React.mixins.add("modelXHRAware",{getInitialState:function(){this.modelOn(s,function(e,t){f({loading:!0},this);var n=this.getModel();t.on("success",function(){f({loading:n[d]},this)},this),t.on("error",function(e){f({loading:n[d],error:e},this)},this)});var e=this.getModel();return{loading:e&&e[d]}},componentDidMount:function(){var e=this.state,t=this.getModel();if(t){var n=t[d];n?(this.modelOnce(l,function(){f({loading:!1},this)}),e.loading||f({loading:!0},this)):e.loading&&f({loading:!1},this)}}},"modelEventAware"),React.mixins.add("modelInvalidAware",{getInitialState:function(){var e=n(this);return e&&this.modelOn("invalid",function(t,n){var i=this.modelIndexErrors(n)||{},a=i&&i[e];a&&f({invalid:a},this)}),{}}},"modelIndexErrors","modelEventAware"),React.mixins.add("modelIndexErrors",{modelIndexErrors:function(e){if(Array.isArray(e)){var t={};return _.each(e,function(e){for(var n in e)t[n]=e[n]}),t}return e}}),React.mixins.add("modelLoadOn",function(){var e=arguments.length>0?Array.prototype.slice.call(arguments,0):void 0;return{getInitialState:function(){e=a(e||"loadOn",this,s+":{key}",function(e){var t=this.getModel();f({loading:t[d]},this),e.on("complete",function(){f({loading:!1},this)},this)});var t=this.getModel();if(t){var n,i=t.loading;if(i)for(var r=function(){f({loading:!1},this)},o=0;o<i.length;o++){var c=e.indexOf(i[o].method);if(c>=0)return n=e[c],i[o].on("complete",r,this),{loading:t[d]}}}return{}},loadWhile:function(e){function t(t){var i=e[t];e[t]=function(){f({loading:!1},n),i&&i.apply(this,arguments)}}e=e||{};var n=this;return t("error"),t("success"),f({loading:!0},this),e}}},"modelEventAware"),React.mixins.add("modelUpdateOn",function(){var e=arguments.length>0?Array.prototype.slice.call(arguments,0):void 0;return{getInitialState:function(){a(e||"updateOn",this,"{key}",function(){this.deferUpdate()})}}},"modelEventAware","deferUpdate"),React.events){React.events.mixin=React.events.mixin||Backbone.Events;var v=/^model(\[.+\])?$/;React.events.handle(v,function(e,t){return{on:function(){this.modelOn(e.path,t)},off:function(){}}});var p=React.events.specials;if(p){var m=["memoize","delay","defer","throttle","debounce","once","after","before"];_.each(m,function(e){p[e]=p[e]||function(t,n){return n=c(n),n.splice(0,0,t),_[e].apply(_,n)}})}}var g=function(e,t,n,i){return React.createClass(_.extend({mixins:["modelAware"],render:function(){var i={},a=h(this);return n?i.defaultChecked=a:i.defaultValue=a,React.DOM[e](_.extend(i,t,this.props),this.props.children)},getValue:function(){if(this.isMounted()){if(n){var e=this.getDOMNode();return e.checked&&(e.value||!0)||!1}return $(this.getDOMNode()).val()}},getDOMValue:function(){return this.isMounted()?$(this.getDOMNode()).val():void 0}},i))};Backbone.input=Backbone.input||{},_.defaults(Backbone.input,{Text:g("input",{type:"text"}),TextArea:g("textarea"),Select:g("select",void 0,void 0),CheckBox:g("input",{type:"checkbox"},!0),RadioGroup:React.createClass({render:function(){var e=this.props;return e.ref="input",React.DOM[e.tag||"span"](e,e.children)},componentDidMount:function(){var e=h(this);if(e){var t='input[value="'+e.replace('"','\\"')+'"]',n=$(this.getDOMNode()).find(t);n.attr("checked","checked")}},getValue:function(){if(this.isMounted())for(var e='input[type="radio"]',t=$(this.getDOMNode()).find(e),n=0;n<t.length;n++)if(t[n].checked)return t[n].value},getDOMValue:function(){if(this.isMounted()){return $(this.getDOMNode()).val()}}})})}()});