-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone-viewport.min.js
6 lines (6 loc) · 2.92 KB
/
backbone-viewport.min.js
1
2
3
4
5
6
/*! backbone-viewport - v0.5.3 - 2017-01-12
* https://github.com/dzhdmitry/backbone-viewport
* Copyright (c) 2017 Dmitry Dzhuleba;
* Licensed
*/
!function(a,b){"function"==typeof define&&define.amd?define(["underscore","backbone"],b):a.Viewport=b(a._,a.Backbone)}(this,function(a,b){var c={};return a?b?(c.View=b.View.extend({tagName:"div",initialize:function(){this.listenTo(this.model,"render",this.render),this.listenTo(this.model,"change:active",this.toggleActive)},template:function(a){return""},render:function(){var a=this.template(this.model.toJSON());return this.$el.html(a),this.trigger("rendered"),this},toggle:function(a){var b=a?"block":"none";this.$el.css("display",b)},toggleActive:function(){this.toggle(this.model.get("active"))}}),c.Model=b.Model.extend({idAttribute:"uri",defaults:{active:!1},getFetchOptions:function(){return{}},url:function(){return"/"+this.get("uri")},show:function(){this.set("active",!0),this.hasChanged("active")&&this.trigger("shown")},hide:function(){this.set("active",!1),this.hasChanged("active")&&this.trigger("hidden")}}),c.Collection=b.Collection.extend({model:c.Model,view:c.View,createView:function(b){function c(a){var b=new d.view({model:a});d.el.append(b.render().el),b.toggleActive()}var d=this;a.isArray(b)?a.each(b,c):c(b)},addPage:function(a,b){var c=this.add(a);return b&&this.createView(c),c},mergePage:function(a,b){var c=this.add(a,{merge:!0});return b?this.createView(c):c.trigger("render"),c},pushPage:function(b,c){var d={active:!0},e=a.extend({},d,b),f=new this.model(e),g=new this.view({model:f});return g.setElement(c),this.add(f),f},open:function(a){this.each(function(b){b.get("uri")==a?b.show():b.hide()})}}),c.Router=b.Router.extend({collection:c.Collection,initialize:function(d){var e={el:b.$("body"),start:!0,pushState:!1,silent:!1,root:"/",pages:[]},f=a.extend({},e,d);this.pushState=f.pushState,this.silent=f.silent,this.root=f.root,this.pages=new this.collection,this.pages.el=f.el,this.listenTo(this.pages,"reset",function(a){a.each(function(b){a.createView(b)})}),this.pages.reset(f.pages),f.start&&this.start(),c.Router.__super__.initialize.call(this,d)},start:function(c){var d={pushState:this.pushState,root:this.root,silent:this.silent},e=a.extend({},d,c);b.history.start(e)},stop:function(){b.history.stop()},getCurrentRoute:function(){return this.pushState?b.history.getPath():b.history.getHash()},go:function(b,c){function d(){h.open(g.uri)}function e(b,c){var e={success:function(a){h.mergePage(a,c),d()}};b.fetch(a.extend(e,b.getFetchOptions()))}var f=this.getCurrentRoute(),g=a.extend({uri:f},b),h=this.pages,i=a.extend({},{force:!1,load:!1},c);if(this.pages.has(g.uri))if(i.force)if(i.load){var j=this.pages.get(g.uri);e(j,!1)}else this.pages.mergePage(g,!1),d();else d();else if(i.load){var k=this.pages.addPage(g,!1);e(k,!0)}else this.pages.addPage(g,!0),d()}}),c):void console.error("Backbone is required by Backbone-Viewport"):void console.error("Underscore is required by Backbone-Viewport")});