forked from theironcook/Backbone.ModelBinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackbone.CollectionBinder.min.js
5 lines (4 loc) · 5.49 KB
/
Backbone.CollectionBinder.min.js
1
2
3
4
5
// Backbone.CollectionBinder v1.1.0
// (c) 2015 Bart Wood
// Distributed Under MIT License
!function(e){"function"==typeof define&&define.amd?define(["underscore","jquery","backbone","Backbone.ModelBinder"],e):"undefined"!=typeof module&&module.exports?module.exports=e(require("underscore"),require("jquery"),require("backbone")):e(_,$,Backbone)}(function(e,t,i){if(!i)throw"Please include Backbone.js before Backbone.ModelBinder.js";if(!i.ModelBinder)throw"Please include Backbone.ModelBinder.js before Backbone.CollectionBinder.js";i.CollectionBinder=function(t,n){if(e.bindAll.apply(e,[this].concat(e.functions(this))),this._elManagers={},this._elManagerFactory=t,!this._elManagerFactory)throw"elManagerFactory must be defined.";this._elManagerFactory.trigger=this.trigger,this._options=e.extend({},i.CollectionBinder.options,n)},i.CollectionBinder.SetOptions=function(e){i.CollectionBinder.options=e},i.CollectionBinder.VERSION="1.1.0",e.extend(i.CollectionBinder.prototype,i.Events,{bind:function(e,t){if(this.unbind(),!e)throw"collection must be defined";if(!t)throw"parentEl must be defined";this._collection=e,this._elManagerFactory._setParentEl(t),this._onCollectionReset(),this._collection.on("add",this._onCollectionAdd,this),this._collection.on("remove",this._onCollectionRemove,this),this._collection.on("reset",this._onCollectionReset,this),this._collection.on("sort",this._onCollectionSort,this)},unbind:function(){void 0!==this._collection&&(this._collection.off("add",this._onCollectionAdd),this._collection.off("remove",this._onCollectionRemove),this._collection.off("reset",this._onCollectionReset),this._collection.off("sort",this._onCollectionSort)),this._removeAllElManagers()},getManagerForEl:function(t){var i,n,o=e.values(this._elManagers);for(i=0;i<o.length;i++)if(n=o[i],n.isElContained(t))return n;return void 0},getManagerForModel:function(t){return this._elManagers[e.isObject(t)?t.cid:t]},_onCollectionAdd:function(e,t,i){var n=this._elManagers[e.cid]=this._elManagerFactory.makeElManager(e);n.createEl();var o=i&&i.at;this._options.autoSort&&null!=o&&o<this._collection.length-1&&this._moveElToPosition(n.getEl(),o)},_onCollectionRemove:function(e){this._removeElManager(e)},_onCollectionReset:function(){this._removeAllElManagers(),this._collection.each(function(e){this._onCollectionAdd(e)},this),this.trigger("elsReset",this._collection)},_onCollectionSort:function(){this._options.autoSort&&this.sortRootEls()},_removeAllElManagers:function(){e.each(this._elManagers,function(e){e.removeEl(),delete this._elManagers[e._model.cid]},this),delete this._elManagers,this._elManagers={}},_removeElManager:function(e){void 0!==this._elManagers[e.cid]&&(this._elManagers[e.cid].removeEl(),delete this._elManagers[e.cid])},_moveElToPosition:function(e,t){var i=this._collection.at(t+1);if(i){var n=this.getManagerForModel(i);if(n){var o=n.getEl();o&&(e.detach(),e.insertBefore(o))}}},sortRootEls:function(){this._collection.each(function(e,i){var n=this.getManagerForModel(e);if(n){var o=n.getEl(),l=t(this._elManagerFactory._getParentEl()).children();l[i]!==o[0]&&(o.detach(),o.insertBefore(l[i]))}},this)}}),i.CollectionBinder.ElManagerFactory=function(t,i){if(e.bindAll.apply(e,[this].concat(e.functions(this))),this._elHtml=t,this._bindings=i,!e.isFunction(this._elHtml)&&!e.isString(this._elHtml))throw"elHtml must be a compliled template or an html string"},e.extend(i.CollectionBinder.ElManagerFactory.prototype,{_setParentEl:function(e){this._parentEl=e},_getParentEl:function(){return this._parentEl},makeElManager:function(n){var o={_model:n,createEl:function(){if(this._el=t(e.isFunction(this._elHtml)?this._elHtml({model:this._model.toJSON()}):this._elHtml),t(this._parentEl).append(this._el),this._bindings)if(e.isString(this._bindings))this._modelBinder=new i.ModelBinder,this._modelBinder.bind(this._model,this._el,i.ModelBinder.createDefaultBindings(this._el,this._bindings));else{if(!e.isObject(this._bindings))throw"Unsupported bindings type, please use a boolean or a bindings hash";this._modelBinder=new i.ModelBinder,this._modelBinder.bind(this._model,this._el,this._bindings)}this.trigger("elCreated",this._model,this._el)},removeEl:function(){void 0!==this._modelBinder&&this._modelBinder.unbind(),this._el.remove(),this.trigger("elRemoved",this._model,this._el)},isElContained:function(e){return this._el===e||t(this._el).has(e).length>0},getModel:function(){return this._model},getEl:function(){return this._el}};return e.extend(o,this),o}}),i.CollectionBinder.ViewManagerFactory=function(t){if(e.bindAll.apply(e,[this].concat(e.functions(this))),this._viewCreator=t,!e.isFunction(this._viewCreator))throw"viewCreator must be a valid function that accepts a model and returns a backbone view"},e.extend(i.CollectionBinder.ViewManagerFactory.prototype,{_setParentEl:function(e){this._parentEl=e},_getParentEl:function(){return this._parentEl},makeElManager:function(i){var n={_model:i,createEl:function(){this._view=this._viewCreator(i),this._view.render(this._model),t(this._parentEl).append(this._view.el),this.trigger("elCreated",this._model,this._view)},removeEl:function(){void 0!==this._view.close?this._view.close():(this._view.$el.remove(),console&&console.log&&console.log("warning, you should implement a close() function for your view, you might end up with zombies")),this.trigger("elRemoved",this._model,this._view)},isElContained:function(e){return this._view.el===e||this._view.$el.has(e).length>0},getModel:function(){return this._model},getView:function(){return this._view},getEl:function(){return this._view.$el}};return e.extend(n,this),n}})});