-
Notifications
You must be signed in to change notification settings - Fork 1
/
backbone.rebar.min.js
6 lines (6 loc) · 9.55 KB
/
backbone.rebar.min.js
1
2
3
4
5
6
/**
* Backbone.Rebar v1.1.0
* Extends the Backbone library with view transitions, subviews, view mediators, local storage for sync, dynamic route definitions, controllers, and a simple log wrapper.
* https://github.com/resource/backbone.rebar
*/
(function(t,e,i){"use strict";var o=this.Rebar=t.Rebar={};Object.create||(Object.create=function(t){function e(){}if(arguments.length>1)throw Error("Object.create implementation only accepts the first parameter.");return e.prototype=t,new e});var s=function(t){var i=this,o=function(){i.apply(this,arguments)};return o.prototype=e.extend(i.prototype,t),o},n=o.Application=function(t){return n.instance&&t&&!t._bypassSingleton?n.instance:(t&&t.logLevel?h.setLogLevel(t.logLevel):h.setLogLevel(h.Levels.None),n.instance=this,this.options=t?t:{},this.state=n.States.Initialized,i(window).on("beforeunload unload",i.proxy(function(){this.state=n.States.Shutdown},this)),void 0)};n.States={Default:0,Initialized:1,Faulted:2,Started:3,Shutdown:4},n.prototype=Object.create(t.Events,{_state:{value:n.States.Default,writable:!0},state:{get:function(){return this._state},set:function(t){this._state!==t&&(this._state=t,this._state===n.States.Initialized&&(e.isFunction(this.createModel)&&this.createModel(this.options.modelOptions),e.isFunction(this.createController)&&this.createController(this.options.controllerOptions),e.isFunction(this.createView)&&this.createView(this.options.viewOptions),e.isFunction(this.createRouter)&&this.createRouter(this.options.routerOptions),this.initialize(this.options)),this.trigger("applicationStateDidChange",this.state))}},initialize:{value:function(){},writable:!0},createModel:{value:function(i){this.model||(this.model=new t.Model(e.extend({},i)))},writable:!0},createView:{value:function(t){this.view||(this.view=new a(e.extend({el:i("#application"),model:this.model,controller:this.controller},t)))},writable:!0},createController:{value:function(t){this.controller||(this.controller=new u(e.extend({model:this.model},t)))},writable:!0},createRouter:{value:function(t){this.router||(this.router=new c(e.extend({landing:this.options.landing?this.options.landing:"",dispatcher:this},t)),this.router.on("routeDidChange",function(t){this.trigger("routeDidChange",t)},this))},writable:!0},startup:{value:function(){if(this.options.bootstrap){var o=this;i.ajax({cache:!1,dataType:"json",type:"GET",url:this.options.bootstrap,success:function(i){e.isEmpty(i)?(o.error="Error: Bootstrap load error.",o.state=n.States.Faulted):(o.model.set("bootstrap",i),t.history.start({pushState:!1}),o.state=n.States.Started)},error:function(t){o.error=t,o.state=n.States.Faulted}})}else t.history.start({pushState:!1}),this.state=n.States.Started}}}),n.extend=s,o.PersistenceModel=t.Model.extend({getStoargeId:function(){var t="pm";return this.urlRoot&&(t=t+"_"+this.url().split("/")[0]),t},set:function(i,o,s){"url"===i?this.urlRoot=o:e.isObject(i)&&e.has(i,"url")&&!e.isUndefined(i.url)&&(this.urlRoot=i.url),t.Model.prototype.set.call(this,i,o,s)},sync:function(t,i,o){if("read"===t)try{this.pullLocalStore(i,o)}catch(s){console.error("Error reading from local store "+i.getStoargeId(),s)}else if("create"===t){var n;try{var r=e.omit(i.attributes,["url","urlRoot"]);n=JSON.stringify(r)}catch(s){console.error("Error writing item to local store "+i.getStoargeId(),s)}localStorage.setItem(i.getStoargeId(),n)}else if("update"===t)try{this.pullLocalStore(i,o)}catch(s){console.error("Error updating model from local store "+i.getStoargeId(),s)}else{if("patch"===t)throw"'patch' not implemented yet";if("delete"===t)throw"'delete' not implemented yet"}},pullLocalStore:function(t,e){if(localStorage){var i=localStorage.getItem(t.getStoargeId());if(null!==i){var o=JSON.parse(i);t.set(o),t.trigger("sync",t,o,e)}}else{var s="Error: 'localStorage' is not supported";e.error&&e.error(t,s,e),t.trigger("sync",t,s,e)}}});var r=o.View=function(i){t.View.call(this,i),e.extend(this,e.pick(this.options,["render","destroy","transitionIn","transitionOut","controller"]))};r.prototype=Object.create(t.View.prototype,{isDestroyed:{get:function(){return e.isUndefined(this._isDestroyed)?!1:this._isDestroyed}},controller:{value:void 0,writable:!0},destroy:{value:function(){this._isDestroyed||(this._isDestroyed=!0,this.trigger("viewDidDestroy",this),this.off(),this.$el.off(),this.remove())},writable:!0},render:{value:function(){},writable:!0},transitionIn:{value:function(t,i){e.isFunction(t)&&t.call(i?i:this)},writable:!0},transitionOut:{value:function(t,i){e.isFunction(t)&&t.call(i?i:this)},writable:!0}}),r.extend=t.View.extend;var a=o.CompositeView=function(t){this.subViews=[],r.call(this,t)};a.prototype=Object.create(r.prototype,{addSubView:{value:function(t){t.on("viewDidDestroy",function(t){this.removeSubView(t)},this),this.subViews.push(t);var e=this;t.render(function(i){var o=i?i:t.el;e.$el.append(o)})},writable:!0},addSubViews:{value:function(t){e.each(t,function(t){this.addSubView(t)},this)},writable:!0},removeSubView:{value:function(t){t.cid||(t=e.where(this.subViews,{cid:t})[0]),this.destroySubView(t),this.subViews=e.reject(this.subViews,function(e){return e.cid===t.cid})},writable:!0},removeAllSubViews:{value:function(){e.each(this.subViews,function(t){this.removeSubView(t)},this)},writable:!0},destroy:{value:function(){this.subViews.length>0&&e.each(this.subViews,function(t){this.destroySubView(t)},this),this.subViews=[],r.prototype.destroy.call(this)},writable:!0},destroySubView:{value:function(t){e.isFunction(t.destroy)&&!t.isDestroyed?t.destroy(!0):e.isUndefined(t.cid)||r.prototype.destroy.call(t)},writable:!0}}),a.extend=r.extend;var l=o.Mediator=function(t){t&&(this.options=t,e.extend(this,e.pick(this.options,["initialize","handle","view"])),this.options.events&&this.processEvents(this.options.events),this.view&&this.options.viewEvents&&this.processViewEvents(this.options.viewEvents)),this._views=[],this.initialize(t)};l.prototype=Object.create(t.Events,{_views:{value:void 0,writable:!0,configurable:!1},view:{value:void 0,writable:!0,configurable:!1},initialize:{value:function(){},writable:!0},addView:{value:function(t,i){var o;o=i?i.split(" "):["all"],e.each(o,function(e){t.on(e,function(i){this.handle(e,t,i)},this)},this),this._views.push(t)},writable:!0},removeView:{value:function(t){t.off(null,this.handle,this),this._views=e.reject(this._views,function(e){return e.cid===t.cid?!0:!1},this)},writable:!0},hasView:{value:function(t){return e.where(this._views,{cid:t.cid})[0]},writable:!0},getView:{value:function(t){return e.filter(this._views,function(i){var o=e.keys(t)[0],s=e.values(t)[0];return i[o]&&i[o]===s?!0:i.options&&i.options[o]&&i.options[o]===s?!0:!1})[0]},writable:!0},getViewByName:{value:function(t){var i=this.getView({name:t});return e.isUndefined(i)&&console.warn("Property 'name' was not found on any views."),i},writable:!0},destroy:{value:function(){e.each(this._views,function(t){this.removeView(t)},this)},writable:!0},handle:{value:function(){},writable:!0},processEvents:{value:function(t){for(var i in t){var o=t[i];e.isObject(o)?this.assignCallbackToDispatcher(i,o.callback,o.dispatcher):this.assignCallbackToDispatcher(i,o,this.options.dispatcher)}}},processViewEvents:{value:function(t){var i=t.split(" ");e.each(i,function(t){this.view.on(""+t,function(e){this.handle(t,this.view,e)},this)},this)}},assignCallbackToDispatcher:{value:function(t,e,i){this[e]?i.on(t,this[e],this):this.options[e]?i.on(t,this.options[e],this):console.error("Error: No method '"+e+"' found on mediator")}}}),l.extend=s;var u=o.Controller=function(t){e.isUndefined(t)||(this.options=t,e.extend(this,e.pick(this.options,["model"])))};u.prototype=Object.create(t.Events,{model:{value:void 0,writable:!0}}),u.extend=s;var c=o.DependencyRouter=function(i){if(!e.isUndefined(i)&&(e.isUndefined(i.landing)||(this.landing=i.landing),!e.isUndefined(i.staticRoutes)))for(var o in i.staticRoutes)this.setStaticRoute(o,i.staticRoutes[o]);t.Router.call(this,i)};c.prototype=Object.create(t.Router.prototype,{landing:{value:"",writable:!0},routes:{value:{"":"handleNoHash","*splat":"handleAll"},writable:!0},staticRoutes:{value:{},writable:!0},handleNoHash:{value:function(){this.handleAll(this.landing+t.history.location.search)},writable:!0},handleAll:{value:function(i){var o=e.isUndefined(i)?t.history.getFragment():i;for(var s in this.staticRoutes)if(s===o)return this.staticRoutes[s](),void 0;var n=this.parseRoute(o);this.trigger("routeDidChange",n),this.pRoute=n},writable:!0},parseRoute:{value:function(t){var e,i,o=t.split("/"),s=o[o.length-1].split("?"),n=s[0].split("#"),r=n[0],a=t.split("#")[1],l=this.parseRouteData(s[1]);if(2===o.length&&(i=o[0]),3===o.length&&(e=o[0],i=o[1]),o.length>3){var u=o.length-2;e="";for(var c=0;u>c;c++)e+=o[c]+(u-1>c?"/":"");i=o[o.length-2]}return{directory:e,file:i,view:""!==r?r:void 0,data:l,anchor:a}},writable:!0},parseRouteData:{value:function(t){if(e.isUndefined(t))return void 0;var i=t.split("&"),o={};return e.each(i,function(t){var e=t.split("=");o[e[0]]=e[1]}),o},writable:!0},getFileLocation:{value:function(t){return e.isUndefined(t.directory)||""===t.directory?e.isUndefined(t.file)||""===t.file?"":t.file:t.directory+"/"+t.file},writable:!0},setStaticRoute:{value:function(t,e){this.staticRoutes[t]=e},writable:!0},setStaticRoutes:{value:function(t){for(var e in t)this.staticRoutes[e]=t[e]},writable:!0}}),c.extend=t.Router.extend;var h=o.Logger=function(){function t(t){n&&r>=h.Levels.Info&&s.console.log(t)}function e(t){n&&r>=h.Levels.Info&&s.console.warn(t)}function i(t){n&&r>=h.Levels.Error&&s.console.error(t)}function o(t){return r=t}var s=this,n=void 0!==typeof console,r=0;return{log:t,warn:e,error:i,setLogLevel:o,Levels:{None:0,Error:10,Info:20,Verbose:30}}}.call(this)}).call(this,Backbone,_,$);