Permalink
Browse files

Fixes GH-190 Updated the remove process to only remove views that hav…

…e rendered or forced
  • Loading branch information...
1 parent 0cd2677 commit cfce2edaaed099865e5ab555b9312e5aca8ce70d @tbranyen committed Oct 26, 2012
Showing with 17 additions and 7 deletions.
  1. +13 −4 backbone.layoutmanager.js
  2. +1 −1 dist/backbone.layoutmanager.min.js
  3. +1 −0 grunt.js
  4. +2 −2 test/views.js
View
@@ -24,6 +24,7 @@ var render = Backbone.View.prototype.render;
// Cache these methods for performance.
var aPush = Array.prototype.push;
var aConcat = Array.prototype.concat;
+var aSplice = Array.prototype.splice;
// LayoutManager is a wrapper around a `Backbone.View`.
var LayoutManager = Backbone.View.extend({
@@ -445,12 +446,16 @@ var LayoutManager = Backbone.View.extend({
// Iterate over all of the nested View's and remove.
root.getViews().each(function(view) {
- LayoutManager._removeView(view, force);
+ // Force doesn't care about if a View has rendered or not.
+ if (view.__manager__.hasRendered || force) {
+ LayoutManager._removeView(view, force);
+ }
});
},
// Remove a single nested View.
_removeView: function(view, force) {
+ var parentViews;
// Shorthand the manager for easier access.
var manager = view.__manager__;
// Test for keep.
@@ -465,16 +470,20 @@ var LayoutManager = Backbone.View.extend({
// Remove the View completely.
view.$el.remove();
+ // Bail out early if no parent exists.
if (!manager.parent) { return; }
+ // Assign (if they exist) the sibling Views to a property.
+ parentViews = manager.parent.views[manager.selector];
+
// If this is an array of items remove items that are not marked to
// keep.
- if (_.isArray(manager.parent.views[manager.selector])) {
+ if (_.isArray(parentViews)) {
// Remove directly from the Array reference.
- return manager.parent.getView(function(view, i) {
+ return _.each(parentViews, function(view, i) {
// If the managers match, splice off this View.
if (view.__manager__ === manager) {
- manager.parent.views[manager.selector].splice(i, 1);
+ aSplice.call(parentViews, i, 1);
}
});
}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -1,4 +1,5 @@
module.exports = function(grunt) {
+ "use strict";
grunt.initConfig({
pkg: "<json:package.json>",
View
@@ -1287,7 +1287,7 @@ asyncTest("Views intermittently render multiple times", 1, function() {
main.setView(".view0", new View1());
- main.render().done(function() {
+ //main.render().done(function() {
main.insertViews({
".view1": [
new View2({ collection: collection }),
@@ -1297,7 +1297,7 @@ asyncTest("Views intermittently render multiple times", 1, function() {
equal(main.$(".listItem").length, 5, "Only five list items");
start();
});
- });
+ //});
});
// https://github.com/tbranyen/backbone.layoutmanager/issues/160

0 comments on commit cfce2ed

Please sign in to comment.