Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

only allow force inside _removeView if the view contains a parent

  • Loading branch information...
commit e90f86ff40e36a679ffdcd2f1bc7b1a7ce61ee5e 1 parent df8d0a5
@tbranyen authored
View
2  backbone.layoutmanager.js
@@ -696,6 +696,8 @@ var LayoutManager = Backbone.View.extend({
var manager = view.__manager__;
// Test for keep.
var keep = _.isBoolean(view.keep) ? view.keep : view.options.keep;
+ // Only allow force if View contains a parent.
+ force = force && manager.parent;
// Only remove views that do not have `keep` attribute set, unless the
// force flag is set.
View
2  dist/backbone.layoutmanager.min.js
@@ -3,4 +3,4 @@
* Copyright 2012, Tim Branyen (@tbranyen)
* backbone.layoutmanager.js may be freely distributed under the MIT license.
*/
-(function(a){"use strict";var b,c=a.Backbone,d=a._,e=a.$,f=c.View.prototype._configure,g=c.View.prototype.render,h=c.View.extend({constructor:function(a){a=a||{},h.setupView(this,a),c.View.call(this,a)},swapLayout:function(a){return a.views=d.defaults({},this.views,a.views),a.setElement(this.el),a},insertView:function(a,b){return b?this.setView(a,b,!0):this.setView(a,!0)},insertViews:function(a){return d.each(a,function(b,c){a[c]=[].concat(b)}),this.setViews(a)},getView:function(a){return this.getViews(a).first().value()},getViews:function(a){var b=d.chain(this.views).map(function(a){return[].concat(a)},this).flatten().value();return d.chain(d.filter(b,a?a:d.identity))},setView:function(a,b,c){var e,f,g=this;d.isString(a)||(c=b,b=a,a=""),this.views=this.views||{},!c&&this.views[a]&&(d.isArray(this.views[a])?d.each(this.views[a],function(a){a.remove()}):this.views[a].remove());if(!b.__manager__)throw new Error("manage property not set. http://tbranyen.github.com/backbone.layoutmanager/#usage/structuring-a-view");return f=b._options(),b.__manager__||h.setupView(b,f),b.render=function(e){function k(){(!c||b.keep||!j.hasRendered)&&f.partial(g.el,a,b.el,c),b.delegateEvents(),j.handler&&(j.handler.resolveWith(b,[b.el]),delete j.handler),i.resolveWith(b,[b.el]),d.isFunction(e)&&e.call(b,b.el)}var i=f.deferred(),j=b.__manager__;return j.viewDeferred=i,b._removeViews(),h.prototype.render.call(b).then(k),i.promise()},b.__manager__.parent=g,b.__manager__.selector=a,c?(e=this.views[a]=this.views[a]||[],d.isArray(this.views[a])||(e=this.views[a]=[this.views[a]]),d.indexOf(e,b)>-1?b:(e.push(b),b.__manager__.append=!0,b)):this.views[a]=b},setViews:function(a){return d.each(a,function(a,b){if(d.isArray(a))return d.each(a,function(a){this.insertView(b,a)},this);this.setView(b,a)},this),this},render:function(a){var b=this,c=this._options(),e=c.deferred(),f=this.__manager__;return f.renderDeferred?(f.callback=a,f.renderDeferred):(f.renderDeferred=e,this._render(h._viewRender).fetch.then(function(){var a=d.map(b.views,function(a){function e(a,b){if(!a.length)return b();var c=a.shift();c.render(function(){e(a,b)})}var b;return d.isArray(a)?(b=c.deferred(),e(d.clone(a),function(){b.resolve()}),b.promise()):a.render()});c.when(a).then(function(){e.resolveWith(b,[b.el])})}),e.then(function(){d.isFunction(a)&&a.call(b,b.el),b.__manager__.handler&&(b.__manager__.handler.resolveWith(b,[b.el]),delete b.__manager__.handler),d.isFunction(b.__manager__.callback)&&(b.__manager__.callback.call(b,b.el),delete b.__manager__.callback),delete b.__manager__.renderDeferred}))},remove:function(){return h.cleanViews(this),h._removeView(this,!0),this._remove.apply(this,arguments)},_options:function(){return d.extend({},this,h.prototype.options,this.options)}},{_cache:{},_makeAsync:function(a,b){var c=a.deferred();return c.async=function(){return c._isAsync=!0,b},c},_viewRender:function(a){function g(c,d){h.cache(b,d),d&&f.html(a.el,f.render(d,c)),e.fetch.resolveWith(a,[a.el])}var b,c,e,f=a._options();return{render:function(i){var j=a.__manager__,k=a.template||f.template;return a.serialize&&(f.serialize=a.serialize),!i&&d.isFunction(f.serialize)?i=f.serialize.call(a):!i&&d.isObject(f.serialize)&&(i=f.serialize),e=h._makeAsync(f,d.bind(g,a,i)),e.fetch=f.deferred(),j.handler=e,d.isString(k)&&(b=j.prefix+k),(c=h.cache(b))?(g(i,c,b),e):(d.isString(k)?c=f.fetch.call(e,j.prefix+k):k!=null&&(c=f.fetch.call(e,k)),e._isAsync||g(i,c),e)}}},cleanViews:function(a){d.each([].concat(a),function(a){a.unbind(),a.views&&d.each(a.views,function(a){h.cleanViews(a)}),d.isFunction(a.cleanup)&&a.cleanup.call(a)})},cache:function(a,b){if(a in this._cache)return this._cache[a];if(a!=null&&b!=null)return this._cache[a]=b},configure:function(a){d.extend(h.prototype.options,a),a.manage&&(c.View.prototype.manage=!0)},setupView:function(a,e){var f,g,i=c.LayoutManager.prototype,j=d.pick(a,b);if(a.__manager__)return;d.defaults(a,{views:{},__manager__:{},_options:h.prototype._options,_removeViews:h._removeViews,_removeView:h._removeView}),a instanceof c.Layout?a.__manager__.prefix=a._options().paths.layout||"":a.__manager__.prefix=a._options().paths.template||"",e=a.options=d.defaults(e||{},a.options,i.options),g=d.pick(e,["events"].concat(d.values(e.events))),d.extend(a,g),delete j.render,d.extend(e,j),a._remove=c.View.prototype.remove,a._render=function(a){var b,c=this._options().beforeRender,f=this._options().afterRender;return this._removeViews(),d.isFunction(c)&&c.call(this,this),this.trigger("beforeRender",this),b=a(this).render(),b.then(function(){var a=this,b=a.__manager__,c=b.parent,g=function(){a.delegateEvents(),a.__manager__.hasRendered=!0,d.isFunction(f)&&f.call(a,a),a.trigger("afterRender",a)},h=function(a){var b=a.__manager__;return b.parent&&!b.parent.__manager__.hasRendered?b.parent:a};if(!c)return g.call(a);if(c.__manager__.hasRendered)return e.when([b.viewDeferred,c.__manager__.viewDeferred]).then(function(){g.call(a)});c=h(a),c.on("afterRender",function(){c.off(null,null,a),g.call(a)},a)}),b},a.render=h.prototype.render,a.remove!==i.remove&&(a._remove=a.remove,a.remove=i.remove),f=e.views||a.views,d.keys(f).length&&a.setViews(f),a.template&&(e.template=a.template,delete a.template)},_removeViews:function(a){a=a||this,a.getViews().each(function(a){h._removeView(a,!0)})},_removeView:function(a,b){var c=a.__manager__,e=d.isBoolean(a.keep)?a.keep:a.options.keep;if(!e&&(c.append===!0||b)&&c.hasRendered){a.$el.remove();if(d.isArray(c.parent.views[c.selector]))return c.parent.getView(function(a,b){a.__manager__.selector===c.selector&&c.parent.views[c.selector].splice(b,1)});delete c.parent.views[c.selector]}}});d.each(["get","set","insert"],function(a){var b=c.View.prototype,d=h.prototype;b[a+"View"]=d[a+"View"],b[a+"Views"]=d[a+"Views"]}),c.Layout=c.LayoutManager=h,c.LayoutView=c.View.extend({manage:!0}),c.View.prototype._configure=function(){var a=f.apply(this,arguments);return this.manage&&h.setupView(this),a},h.prototype.options={paths:{},deferred:function(){return e.Deferred()},fetch:function(a){return d.template(e(a).html())},partial:function(a,b,c,d){var f=b?e(a).find(b):e(a);return f.length?(this[d?"append":"html"](f,c),!0):!1},html:function(a,b){e(a).html(b)},append:function(a,b){e(a).append(b)},when:function(a){return e.when.apply(null,a)},render:function(a,b){return a(b)}},b=d.keys(h.prototype.options)})(this);
+(function(a){"use strict";var b,c=a.Backbone,d=a._,e=a.$,f=c.View.prototype._configure,g=c.View.prototype.render,h=c.View.extend({constructor:function(a){a=a||{},h.setupView(this,a),c.View.call(this,a)},swapLayout:function(a){return a.views=d.defaults({},this.views,a.views),a.setElement(this.el),a},insertView:function(a,b){return b?this.setView(a,b,!0):this.setView(a,!0)},insertViews:function(a){return d.each(a,function(b,c){a[c]=[].concat(b)}),this.setViews(a)},getView:function(a){return this.getViews(a).first().value()},getViews:function(a){var b=d.chain(this.views).map(function(a){return[].concat(a)},this).flatten().value();return d.chain(d.filter(b,a?a:d.identity))},setView:function(a,b,c){var e,f,g=this;d.isString(a)||(c=b,b=a,a=""),this.views=this.views||{},!c&&this.views[a]&&(d.isArray(this.views[a])?d.each(this.views[a],function(a){a.remove()}):this.views[a].remove());if(!b.__manager__)throw new Error("manage property not set. http://tbranyen.github.com/backbone.layoutmanager/#usage/structuring-a-view");return f=b._options(),b.__manager__||h.setupView(b,f),b.render=function(e){function k(){(!c||b.keep||!j.hasRendered)&&f.partial(g.el,a,b.el,c),b.delegateEvents(),j.handler&&(j.handler.resolveWith(b,[b.el]),delete j.handler),i.resolveWith(b,[b.el]),d.isFunction(e)&&e.call(b,b.el)}var i=f.deferred(),j=b.__manager__;return j.viewDeferred=i,b._removeViews(),h.prototype.render.call(b).then(k),i.promise()},b.__manager__.parent=g,b.__manager__.selector=a,c?(e=this.views[a]=this.views[a]||[],d.isArray(this.views[a])||(e=this.views[a]=[this.views[a]]),d.indexOf(e,b)>-1?b:(e.push(b),b.__manager__.append=!0,b)):this.views[a]=b},setViews:function(a){return d.each(a,function(a,b){if(d.isArray(a))return d.each(a,function(a){this.insertView(b,a)},this);this.setView(b,a)},this),this},render:function(a){var b=this,c=this._options(),e=c.deferred(),f=this.__manager__;return f.renderDeferred?(f.callback=a,f.renderDeferred):(f.renderDeferred=e,this._render(h._viewRender).fetch.then(function(){var a=d.map(b.views,function(a){function e(a,b){if(!a.length)return b();var c=a.shift();c.render(function(){e(a,b)})}var b;return d.isArray(a)?(b=c.deferred(),e(d.clone(a),function(){b.resolve()}),b.promise()):a.render()});c.when(a).then(function(){e.resolveWith(b,[b.el])})}),e.then(function(){d.isFunction(a)&&a.call(b,b.el),b.__manager__.handler&&(b.__manager__.handler.resolveWith(b,[b.el]),delete b.__manager__.handler),d.isFunction(b.__manager__.callback)&&(b.__manager__.callback.call(b,b.el),delete b.__manager__.callback),delete b.__manager__.renderDeferred}))},remove:function(){return h.cleanViews(this),h._removeView(this,!0),this._remove.apply(this,arguments)},_options:function(){return d.extend({},this,h.prototype.options,this.options)}},{_cache:{},_makeAsync:function(a,b){var c=a.deferred();return c.async=function(){return c._isAsync=!0,b},c},_viewRender:function(a){function g(c,d){h.cache(b,d),d&&f.html(a.el,f.render(d,c)),e.fetch.resolveWith(a,[a.el])}var b,c,e,f=a._options();return{render:function(i){var j=a.__manager__,k=a.template||f.template;return a.serialize&&(f.serialize=a.serialize),!i&&d.isFunction(f.serialize)?i=f.serialize.call(a):!i&&d.isObject(f.serialize)&&(i=f.serialize),e=h._makeAsync(f,d.bind(g,a,i)),e.fetch=f.deferred(),j.handler=e,d.isString(k)&&(b=j.prefix+k),(c=h.cache(b))?(g(i,c,b),e):(d.isString(k)?c=f.fetch.call(e,j.prefix+k):k!=null&&(c=f.fetch.call(e,k)),e._isAsync||g(i,c),e)}}},cleanViews:function(a){d.each([].concat(a),function(a){a.unbind(),a.views&&d.each(a.views,function(a){h.cleanViews(a)}),d.isFunction(a.cleanup)&&a.cleanup.call(a)})},cache:function(a,b){if(a in this._cache)return this._cache[a];if(a!=null&&b!=null)return this._cache[a]=b},configure:function(a){d.extend(h.prototype.options,a),a.manage&&(c.View.prototype.manage=!0)},setupView:function(a,e){var f,g,i=c.LayoutManager.prototype,j=d.pick(a,b);if(a.__manager__)return;d.defaults(a,{views:{},__manager__:{},_options:h.prototype._options,_removeViews:h._removeViews,_removeView:h._removeView}),a instanceof c.Layout?a.__manager__.prefix=a._options().paths.layout||"":a.__manager__.prefix=a._options().paths.template||"",e=a.options=d.defaults(e||{},a.options,i.options),g=d.pick(e,["events"].concat(d.values(e.events))),d.extend(a,g),delete j.render,d.extend(e,j),a._remove=c.View.prototype.remove,a._render=function(a){var b,c=this._options().beforeRender,f=this._options().afterRender;return this._removeViews(),d.isFunction(c)&&c.call(this,this),this.trigger("beforeRender",this),b=a(this).render(),b.then(function(){var a=this,b=a.__manager__,c=b.parent,g=function(){a.delegateEvents(),a.__manager__.hasRendered=!0,d.isFunction(f)&&f.call(a,a),a.trigger("afterRender",a)},h=function(a){var b=a.__manager__;return b.parent&&!b.parent.__manager__.hasRendered?b.parent:a};if(!c)return g.call(a);if(c.__manager__.hasRendered)return e.when([b.viewDeferred,c.__manager__.viewDeferred]).then(function(){g.call(a)});c=h(a),c.on("afterRender",function(){c.off(null,null,a),g.call(a)},a)}),b},a.render=h.prototype.render,a.remove!==i.remove&&(a._remove=a.remove,a.remove=i.remove),f=e.views||a.views,d.keys(f).length&&a.setViews(f),a.template&&(e.template=a.template,delete a.template)},_removeViews:function(a){a=a||this,a.getViews().each(function(a){h._removeView(a,!0)})},_removeView:function(a,b){var c=a.__manager__,e=d.isBoolean(a.keep)?a.keep:a.options.keep;b=b&&c.parent;if(!e&&(c.append===!0||b)&&c.hasRendered){a.$el.remove();if(d.isArray(c.parent.views[c.selector]))return c.parent.getView(function(a,b){a.__manager__.selector===c.selector&&c.parent.views[c.selector].splice(b,1)});delete c.parent.views[c.selector]}}});d.each(["get","set","insert"],function(a){var b=c.View.prototype,d=h.prototype;b[a+"View"]=d[a+"View"],b[a+"Views"]=d[a+"Views"]}),c.Layout=c.LayoutManager=h,c.LayoutView=c.View.extend({manage:!0}),c.View.prototype._configure=function(){var a=f.apply(this,arguments);return this.manage&&h.setupView(this),a},h.prototype.options={paths:{},deferred:function(){return e.Deferred()},fetch:function(a){return d.template(e(a).html())},partial:function(a,b,c,d){var f=b?e(a).find(b):e(a);return f.length?(this[d?"append":"html"](f,c),!0):!1},html:function(a,b){e(a).html(b)},append:function(a,b){e(a).append(b)},when:function(a){return e.when.apply(null,a)},render:function(a,b){return a(b)}},b=d.keys(h.prototype.options)})(this);
Please sign in to comment.
Something went wrong with that request. Please try again.