-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathBackbone.CollectionBinder.min.js
5 lines (4 loc) · 5.57 KB
/
Backbone.CollectionBinder.min.js
1
2
3
4
5
// Backbone.CollectionBinder v1.1.1
// (c) 2020 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(o,l,i){if(!i)throw new Error("Please include Backbone.js before Backbone.ModelBinder.js");if(!i.ModelBinder)throw new Error("Please include Backbone.ModelBinder.js before Backbone.CollectionBinder.js");i.CollectionBinder=function(e,t){if(o.bindAll.apply(o,[this].concat(o.functions(this))),this._elManagers={},this._elManagerFactory=e,!this._elManagerFactory)throw new Error("elManagerFactory must be defined.");this._elManagerFactory.trigger=this.trigger,this._options=o.extend({},i.CollectionBinder.options,t)},i.CollectionBinder.SetOptions=function(e){i.CollectionBinder.options=e},i.CollectionBinder.VERSION="1.1.1",o.extend(i.CollectionBinder.prototype,i.Events,{bind:function(e,t){if(this.unbind(),!e)throw new Error("collection must be defined");if(!t)throw new Error("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(e){var t,i,n=o.values(this._elManagers);for(t=0;t<n.length;t++)if((i=n[t]).isElContained(e))return i},getManagerForModel:function(e){return this._elManagers[o.isObject(e)?e.cid:e]},_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(){o.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,t){var i=this.getManagerForModel(e);if(i){var n=i.getEl(),o=l(this._elManagerFactory._getParentEl()).children();o[t]!==n[0]&&(n.detach(),n.insertBefore(o[t]))}},this)}}),i.CollectionBinder.ElManagerFactory=function(e,t){if(o.bindAll.apply(o,[this].concat(o.functions(this))),this._elHtml=e,this._bindings=t,!o.isFunction(this._elHtml)&&!o.isString(this._elHtml))throw new Error("elHtml must be a compliled template or an html string")},o.extend(i.CollectionBinder.ElManagerFactory.prototype,{_setParentEl:function(e){this._parentEl=e},_getParentEl:function(){return this._parentEl},makeElManager:function(e){var t={_model:e,createEl:function(){if(this._el=o.isFunction(this._elHtml)?l(this._elHtml({model:this._model.toJSON()})):l(this._elHtml),l(this._parentEl).append(this._el),this._bindings)if(o.isString(this._bindings))this._modelBinder=new i.ModelBinder,this._modelBinder.bind(this._model,this._el,i.ModelBinder.createDefaultBindings(this._el,this._bindings));else{if(!o.isObject(this._bindings))throw new Error("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||0<l(this._el).has(e).length},getModel:function(){return this._model},getEl:function(){return this._el}};return o.extend(t,this),t}}),i.CollectionBinder.ViewManagerFactory=function(e){if(o.bindAll.apply(o,[this].concat(o.functions(this))),this._viewCreator=e,!o.isFunction(this._viewCreator))throw new Error("viewCreator must be a valid function that accepts a model and returns a backbone view")},o.extend(i.CollectionBinder.ViewManagerFactory.prototype,{_setParentEl:function(e){this._parentEl=e},_getParentEl:function(){return this._parentEl},makeElManager:function(e){var t={_model:e,createEl:function(){this._view=this._viewCreator(e),this._view.render(this._model),l(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||0<this._view.$el.has(e).length},getModel:function(){return this._model},getView:function(){return this._view},getEl:function(){return this._view.$el}};return o.extend(t,this),t}})});