-
Notifications
You must be signed in to change notification settings - Fork 1
/
backbone-fiber.min.js
1 lines (1 loc) · 5.41 KB
/
backbone-fiber.min.js
1
(function(t,e){"function"==typeof define&&define.amd?define(["jquery","underscore","backbone","base"],e):t.Backbone.Fiber=e(t.$,t._,t.Backbone,Base)})(this,function(t,e,i,n){function r(i){var n=t.Deferred();return n.then(function(){delete h[i]},function(){delete h[i]}),s[i]?n.resolve(s[i]):(h[i]=n,require([d.viewPath+i,"text!"+d.viewPath+i+".html"],function(t,r){void 0===t||void 0===r?n.reject():(t.prototype.instanceOf=i,t.prototype.template=e.template(r),s[i]=t,n.resolve(t))},function(t){n.reject(t)})),n.promise()}function o(i,n,o){var a=t.Deferred();a.then(function(){delete l[i]},function(){delete l[i]}),l[i]=a,r(i).then(function(t){var i,r=new t(e.extend(o,{el:n[0]})),d=!1;n.attr("data-cid",r.cid),c[r.cid]=r,i=n.parents("[data-view]").first(),i.length>0?(i=i.attr("data-cid"),c[i]?(c[i].addChild(r),r.setParent(c[i])):(d=!0,r.remove())):0===n.closest("body").length&&(d=!0,r.remove()),d?a.reject():(r.render(),a.resolve(r))},function(){a.reject()})}function a(e,i){var n=i||{},r=e instanceof t?e:t(e),a=r.attr("data-view");a&&o(a,r,n)}i.View.extend=n.extend,i.View.mix=n.mix;var d,s={},c={},h={},l={},u=t.cleanData;return t.cleanData=function(e){for(var i,n=0;void 0!==(i=e[n]);n++)t(i).triggerHandler("destroyed");u(e)},i.Fiber=d={viewPath:"views/",getViewFromEl:function(e){var i=t(e),n=i.attr("data-cid")||i.closest("[data-cid]").attr("data-cid");return n?c[n]:void 0},getViewFromCid:function(t){return c[t]},getElFromCid:function(t){return c[t]?c[t].$el:void 0},getPromise:function(t){var e;return(e=l[t]||h[t])?e:void 0},isLoaded:function(e){return e=e instanceof t?e.attr("data-view")||e.closest("[data-view]").attr("data-view"):e,h[e]?!1:s[e]?!0:null}},d.start=function(){a(document.body)},i.View.mix([{_connected:!0,template:null,parent:null,children:null,renderedOnce:!1,forceRender:!1,connect:function(e,i){var n,r,o=this,s=t.Deferred(),c=null;return n="string"==typeof e?this.factory(e,i):e instanceof t?e:t(e),a(n,i),(r=d.getPromise(n.attr("data-view")))?r.then(function(){(c=d.getViewFromEl(n))?s.resolveWith(o,[c]):s.rejectWith(o)},function(){s.rejectWith(o)}):(c=d.getViewFromEl(n))?s.resolveWith(this,[c]):s.rejectWith(this),s.promise()},waitFor:function(i,n,r){var o=this,a=e.compact(e.map(i,function(t){return d.getPromise(t)}));t.when.apply(this,a).then(function(){n.apply(o,arguments)},function(){r&&r.apply(o,arguments)})},factory:function(e){return t("<div>").attr("data-view",e).appendTo(this.$el)},initialize:function(t){this.$el.on("destroyed",e.bind(this.remove,this)),this.options=t||{},this.children=[],this.setup(t),this.bindData(),this.trigger("created")},unbindData:function(){var t=this.data();t.trigger&&this.stopListening(t)},bindData:function(){var i=this,n=this.data();n.trigger&&e.each(this.events,function(e,r){var o;(o=r.indexOf(".data"))>-1&&(e=t.isFunction(e)?e:i[e],i.listenTo(n,r.slice(0,o),e))})},setup:t.noop,render:function(){var e,i,n=this;this.beforeRender()!==!1&&(this.trigger("rendering"),this.template&&"function"==typeof this.template&&(e=this.dataSerialized(),i=e&&t.isArray(e),((this.forceRender||this.renderedOnce)&&e||!this.renderedOnce&&(i&&e.length>0||!i))&&(this.$el.empty().html(this.template(e)),this.renderedOnce=!0,this.$el.find("[data-view]").each(function(e,i){$el=t(i),n.connect($el).fail(function(){n.trigger("child-connect-error",{"data-view":$el.attr("data-view"),$el:$el})})}))),this.afterRender(),this.trigger("rendered"))},beforeRender:t.noop,afterRender:t.noop,data:function(){return this.model?this.model:this.collection?this.collection:{}},clearData:function(){var t=this.data();return this.unbindData(),this.model=null,this.collection=null,this.trigger("undata",t),this},setData:function(t){var e=null;return this.unbindData(),t.model?this.model=e=t.model:t.collection&&(this.collection=e=t.collection),this.bindData(),e&&e.trigger&&e.trigger("ready",e,this),this},dataSerialized:function(){var t=this.data();return t.toJSON?t.toJSON({computedFields:!0}):t},remove:function(){this.parent?c[this.parent].removeChild(this):(this._connected=!1,this.destroy(),this.trigger("removed"),this.stopListening(),this.$el.off("destroyed"),this.$el.remove(),delete c[this.cid]),this._superStop()},destroy:t.noop,trigger:function(t,e){this.$el.trigger(t,{view:this,data:e||{}})||this._superStop()},setParent:function(t){this.parent=null,t&&(this.parent=t.cid)},addChild:function(t){e.contains(t.cid,this.children)||this.children.push(t.cid)},removeChild:function(t){var i=e.indexOf(this.children,t.cid);i>-1&&(this.children.splice(i,1),t.setParent(null),t.remove())},allChildren:function(){return e.map(this.children,function(t){return d.getViewFromCid(t)})},findChildren:function(t){return e.compact(e.map(this.children,function(e){var i;return(i=d.getViewFromCid(e))&&i.instanceOf==t?i:void 0}))},findChild:function(e){var i=null;if(e instanceof t)this.isChildLoaded(e)&&(i=d.getViewFromEl(e));else if("string"==typeof e&&(i=d.getViewFromCid(e),!i))for(var n=0;this.children.length>n;n++){var r;(r=d.getViewFromCid(this.children[n]))&&r.instanceOf==e&&(i=r)}return i},isMyChild:function(t){return t.parent&&void 0!==e.find(this.children,function(e){return t.cid==e})||!t.parent&&this.isMyElement(t.$el.parent())},isChildLoaded:function(t){return d.isLoaded(t)},purgeChildEl:function(e){var i,n=e instanceof t?e:t(e);this.isChildLoaded(n)&&(i=this.findChild(n))?i.remove():n.remove()},isMyElement:function(e){var i=e instanceof t?e:t(e);return i.closest("[data-view]").first().attr("data-cid")==this.cid},isConnected:function(){return this._connected}}]),d});