UNPKG

backbone-fractal

Version:

Lightweight composite views for Backbone

3 lines (2 loc) 7.27 kB
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).BackboneFractal=e()}}(function(){return function r(s,u,c){function f(t,e){if(!u[t]){if(!s[t]){var n="function"==typeof require&&require;if(!e&&n)return n(t,!0);if(l)return l(t,!0);var i=new Error("Cannot find module '"+t+"'");throw i.code="MODULE_NOT_FOUND",i}var o=u[t]={exports:{}};s[t][0].call(o.exports,function(e){return f(s[t][1][e]||e)},o,o.exports,r,s,u,c)}return u[t].exports}for(var l="function"==typeof require&&require,e=0;e<c.length;e++)f(c[e]);return f}({1:[function(f,e,l){(function(e){"use strict";var i,t=this&&this.__extends||(i=function(e,t){return(i=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])})(e,t)},function(e,t){function n(){this.constructor=e}i(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)});Object.defineProperty(l,"__esModule",{value:!0});var n,o="undefined"!=typeof window?window._:void 0!==e?e._:null,r=f("./underscore-compat"),s="undefined"!=typeof window?window.Backbone:void 0!==e?e.Backbone:null,u=(n=s.View,t(c,n),c.prototype.initCollectionEvents=function(){return this.listenTo(this.collection,{add:this.insertItem,remove:this.removeItem,sort:this.sortItems,update:this.placeItems,reset:this.resetItems})},c.prototype.render=function(){return this.beforeRender(),this.detachItems(),this.renderContainer(),this._container=this.container?this.$(this.container).first():this.$el,this.placeItems(),this.afterRender(),this},c.prototype.beforeRender=function(){return this},c.prototype.renderContainer=function(){return this},c.prototype.afterRender=function(){return this},c.prototype.remove=function(){return this.clearItems(),n.prototype.remove.call(this),this},c.prototype.makeItem=function(e){return new this.subview({model:e})},c.prototype.initItems=function(){return this.items=this.collection.map(this.makeItem.bind(this)),this},c.prototype.clearItems=function(){return r.eachRight(this.items,function(e){return e.remove()}),this},c.prototype.insertItem=function(e,t,n){var i=this.makeItem(e),o=n&&n.index;return null!=o?this.items.splice(o,0,i):this.items.push(i),this},c.prototype.removeItem=function(e,t,n){var i=n.index;return this.items[i].remove(),this.items.splice(i,1),this},c.prototype.sortItems=function(){var n=o.invert(o.map(this.collection.models,"cid"));return this.items.sort(function(e,t){return+n[e.model.cid]-+n[t.model.cid]}),this},c.prototype.detachItems=function(){return r.eachRight(this.items,function(e){return e.$el.detach()}),this},c.prototype.placeItems=function(){return this._container.append(o.map(this.items,"el")),this},c.prototype.resetItems=function(){return this.clearItems().initItems().placeItems()},c);function c(){return null!==n&&n.apply(this,arguments)||this}l.default=u}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{"./underscore-compat":4}],2:[function(h,e,d){(function(e){"use strict";var i,t=this&&this.__extends||(i=function(e,t){return(i=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])})(e,t)},function(e,t){function n(){this.constructor=e}i(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)});Object.defineProperty(d,"__esModule",{value:!0});var n,s="undefined"!=typeof window?window._:void 0!==e?e._:null,r=h("./underscore-compat"),u="undefined"!=typeof window?window.Backbone:void 0!==e?e.Backbone:null,c={placeOnly:!1,reverse:!1},o=(n=u.View,t(f,n),f.prototype.render=function(){return this.beforeRender(),this.detachSubviews(),this._resetContainer(),this.placeSubviews(),this.afterRender(),this},f.prototype.beforeRender=function(){return this},f.prototype.renderContainer=function(){return this},f.prototype.afterRender=function(){return this},f.prototype.remove=function(){return this.clearSubviews(),delete this._containers,n.prototype.remove.call(this),this},f.prototype.subviews=function(){return[]},f.prototype.clearSubviews=function(){return this.forEachSubview(l,{reverse:!0})},f.prototype.detachSubviews=function(){return this.forEachSubview(p,{reverse:!0})},f.prototype.placeSubviews=function(){return this.forEachSubview(a,{placeOnly:!0})},f.prototype.forEachSubview=function(t,e){var n=this,i=e||c,o=i.placeOnly;return(i.reverse?r.eachRight:s.each)(this._getSubviews(o),function(e){return t.apply(n,e)}),this},f.prototype._resetContainer=function(){return this._containers={},this.renderContainer()},f.prototype._getSubviews=function(e){var t=this._normalizeSubview.bind(this,e);return this._containers||this._resetContainer(),s.compact(s.map(s.result(this,"subviews"),t))},f.prototype._normalizeSubview=function(e,t){if(t=this._resolve(t),s.isUndefined(t))return null;if(t instanceof u.View)return[t,this.$el,this.defaultPlacement];var n=t.view,i=t.selector,o=t.place;if(e&&!s.isUndefined(o)&&!this._resolve(o))return null;if(!((n=this._resolve(n))instanceof u.View))return null;if(s.isFunction(i)&&(i=i.call(this)),s.isString(i)){var r=t.method;return this._normalizeSelectorSubview(n,i,r)}return r=t.method,this._normalizeRootSubview(n,r)},f.prototype._normalizeRootSubview=function(e,t){if(s.isFunction(t)&&(t=t.call(this)),t&&"append"!==t&&"prepend"!==t)throw new TypeError("Selector empty or attempting jQuery method ."+t+"() on the root element of a CompositeView.");return[e,this.$el,t||this.defaultPlacement]},f.prototype._normalizeSelectorSubview=function(e,t,n){s.isFunction(n)&&(n=n.call(this));var i=this._containers[t];return i||(this._containers[t]=i=this.$(t).first()),[e,i,n||this.defaultPlacement]},f.prototype._resolve=function(e){return s.isString(e)&&(e=s.result(this,e)),s.isFunction(e)&&(e=e.call(this)),e},f);function f(){return null!==n&&n.apply(this,arguments)||this}function l(e){e.remove()}function p(e){e.$el.detach()}function a(e,t,n){t[n].call(t,e.el)}d.default=o,s.extend(o.prototype,{defaultPlacement:"append"}),d._removeSubview=l,d._detachSubview=p,d._placeSubview=a}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{"./underscore-compat":4}],3:[function(e,t,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0});var i=e("./composite-view");n.CompositeView=i.default;var o=e("./collection-view");n.CollectionView=o.default},{"./collection-view":1,"./composite-view":2}],4:[function(e,t,o){(function(e){"use strict";Object.defineProperty(o,"__esModule",{value:!0});var t,n,i="undefined"!=typeof window?window._:void 0!==e?e._:null;(n=t=t||{}).reverse=function(e){for(var t,n=0,i=e.length-1;n<i;)t=e[n],e[n]=e[i],e[i]=t,++n,--i;return e},n.eachRight=function(e,o){return i.reduceRight(e,function(e,t,n,i){return o(t,n,i),e},{}),e},o.reverse=i.reverse||t.reverse,o.eachRight=i.eachRight||t.eachRight}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{}]},{},[3])(3)}); //# sourceMappingURL=backbone-fractal.js.map