"use strict";!function(a,b){if("function"==typeof define&&define.amd)define(["react","backbone","underscore"],b);else if("undefined"!=typeof module&&module.exports){var c=require("react"),d=require("backbone"),e=require("underscore");module.exports=b(c,d,e)}else b(a.React,a.Backbone,a._)}(this,function(a,b,c){function d(a,f){f=f||{};var g,h=f.model,i=f.collection;(c.isElement(f.el)||b.$&&f.el instanceof b.$)&&(g=f.el,delete f.el),"undefined"!=typeof h&&(h.attributes||"object"==typeof h&&c.values(h)[0].attributes)&&(delete f.model,this.model=h,this.setPropsBackbone(h,void 0,f)),"undefined"!=typeof i&&(i.models||"object"==typeof i&&c.values(i)[0].models)&&(delete f.collection,this.collection=i,this.setPropsBackbone(i,void 0,f));var j;a.prototype?(j=this.virtualComponent=c.defaults(a.apply(this,c.rest(arguments)).__realComponentInstance,b.Events,c.omit(b.React.Component,"mixin"),{clone:function(b,c){return new d(a,b,c).virtualComponent},cid:c.uniqueId(),wrapper:this}),g&&e.setElement.call(j,g)):(j=a,this.component=j),j._owner||(this.startModelListeners(),this.startCollectionListeners())}b.React||(b.React={}),b.React.Component={extend:function(f){function g(a,b){return new d(h,a,b).virtualComponent}g.extend=function(){return b.React.Component.extend(c.extend({},f,arguments[0]))},f.mixins?f.mixins[0]!==e&&f.mixins.splice(0,0,e):f.mixins=[e];var h=a.createClass(c.extend(g.prototype,f));return g},mount:function(b,c){if(!b&&!this.el)throw new Error("No element to mount on");return b||(b=this.el),this.wrapper.component=a.renderComponent(this,b,c),this},remove:function(){this.wrapper.component&&this.wrapper.component.isMounted()&&this.unmount(),this.wrapper.stopListening(),this.stopListening()},toHTML:function(){var b=this.clone(c.extend({},this.props,{collection:this.wrapper.collection,model:this.wrapper.model}));return a.renderComponentToString(b)},unmount:function(){var b=this.el.parentNode;if(!a.unmountComponentAtNode(b))throw new Error("There was an error unmounting the component");delete this.wrapper.component,this.setElement(b)}};var e=b.React.Component.mixin={componentDidMount:function(){this.setElement(this.getDOMNode())},componentDidUpdate:function(){this.setElement(this.getDOMNode())},componentWillMount:function(){this.wrapper||(this.isBackboneMixin=!0,this.wrapper=new d(this,this.props))},componentWillUnmount:function(){this.wrapper&&this.isBackboneMixin&&(this.wrapper.stopListening(),delete this.wrapper)},$:function(){return this.$el?this.$el.find.apply(this.$el,arguments):void 0},getCollection:function(){for(var a=this,b=a.wrapper;!b.collection;){if(a=a._owner,!a)throw new Error("Collection not found");b=a.wrapper}return b.collection},getModel:function(){for(var a=this,b=a.wrapper;!b.model;){if(a=a._owner,!a)throw new Error("Model not found");b=a.wrapper}return b.model},getOwner:function(){for(var a=this;a._owner;)a=a._owner;return a},setElement:function(a){if(a&&b.$&&a instanceof b.$){if(a.length>1)throw new Error("You can only assign one element to a component");this.el=a[0],this.$el=a}else a&&(this.el=a,b.$&&(this.$el=b.$(a)));return this}};return c.extend(d.prototype,b.Events,{onError:function(a,b,c){c.silent||this.setProps({isRequesting:!1,hasError:!0,error:b})},onRequest:function(a,b,c){c.silent||this.setProps({isRequesting:!0,hasError:!1})},onSync:function(a,b,c){c.silent||this.setProps({isRequesting:!1})},setPropsBackbone:function(a,b,c){if(a.models||a.attributes)this.setProps.apply(this,arguments);else for(b in a)this.setPropsBackbone(a[b],b,c)},setProps:function(a,d,e){e||this.component&&this.component.isMounted()||(e=this.virtualComponent.props);var f={},g=a.toJSON?a.toJSON():a;d?f[d]=g:a instanceof b.Collection?f.collection=g:f=g,e?c.extend(e,f):(this.nextProps=c.extend(this.nextProps||{},f),c.defer(c.bind(function(){this.nextProps&&(this.component.setProps(this.nextProps),delete this.nextProps)},this)))},startCollectionListeners:function(a,b){if(a||(a=this.collection),a)if(a.models)this.listenTo(a,"add remove change sort reset",c.partial(this.setPropsBackbone,a,b,void 0)).listenTo(a,"error",this.onError).listenTo(a,"request",this.onRequest).listenTo(a,"sync",this.onSync);else if("object"==typeof a)for(b in a)a.hasOwnProperty(b)&&this.startCollectionListeners(a[b],b)},startModelListeners:function(a,b){if(a||(a=this.model),a)if(a.attributes)this.listenTo(a,"change",c.partial(this.setPropsBackbone,a,b,void 0)).listenTo(a,"error",this.onError).listenTo(a,"request",this.onRequest).listenTo(a,"sync",this.onSync);else if("object"==typeof a)for(b in a)this.startModelListeners(a[b],b)}}),b.React.Component});