Remove views (rather than detaching) if Pane::destroyItem is called

This commit is contained in:
Nathan Sobo
2014-01-10 13:35:18 -07:00
parent 7801d8562f
commit 47870a1214
3 changed files with 18 additions and 6 deletions

View File

@@ -196,6 +196,11 @@ describe "Pane", ->
expect(pane.getItems().indexOf(editor2)).not.toBe -1
expect(editor2.isDestroyed()).toBe false
it "removes the item's associated view", ->
view1.remove = (selector, keepData) -> @wasRemoved = not keepData
pane.destroyItem(view1)
expect(view1.wasRemoved).toBe true
describe "::removeItem(item)", ->
it "removes the item from the items list and shows the next item if it was showing", ->
pane.removeItem(view1)

View File

@@ -22,6 +22,8 @@ class PaneModel extends Model
.map((activePane) => activePane is this)
.distinctUntilChanged()
destroyingItem: false
constructor: (params) ->
super
@@ -37,7 +39,7 @@ class PaneModel extends Model
@subscribe @items.onRemoval (item, index) =>
@unsubscribe item if typeof item.on is 'function'
@emit 'item-removed', item, index
@emit 'item-removed', item, index, @destroyingItem
@when @items.$length.becomesLessThan(1), 'destroy'
@@ -145,7 +147,9 @@ class PaneModel extends Model
@emit 'before-item-destroyed', item
if @promptToSaveItem(item)
@emit 'item-destroyed', item
@removeItem(item, options)
@destroyingItem = true
@removeItem(item)
@destroyingItem = false
item.destroy?()
true
else

View File

@@ -149,8 +149,8 @@ class Pane extends View
onItemAdded: (item, index) =>
@trigger 'pane:item-added', [item, index]
onItemRemoved: (item, index) =>
@cleanupItemView(item)
onItemRemoved: (item, index, destroyed) =>
@cleanupItemView(item, destroyed)
@trigger 'pane:item-removed', [item, index]
onItemMoved: (item, newIndex) =>
@@ -168,7 +168,7 @@ class Pane extends View
@trigger 'pane:active-item-title-changed'
# Private:
cleanupItemView: (item) ->
cleanupItemView: (item, destroyed) ->
if item instanceof $
viewToRemove = item
else if viewToRemove = @viewsByItem.get(item)
@@ -176,7 +176,10 @@ class Pane extends View
if viewToRemove?
viewToRemove.setModel?(null)
viewToRemove.detach()
if destroyed
viewToRemove.remove()
else
viewToRemove.detach()
# Private:
viewForItem: (item) ->