Merge pull request #7395 from oggy/will-remove-item

Add Pane::onWillRemoveItem event.
This commit is contained in:
Ben Ogle 2015-06-30 15:25:16 -07:00
commit 4d77d4161d
2 changed files with 24 additions and 0 deletions

View File

@ -209,6 +209,12 @@ describe "Pane", ->
expect(item2.isDestroyed()).toBe true
expect(events).toEqual [{item: item2, index: 1}]
it "invokes ::onWillRemoveItem() observers", ->
events = []
pane.onWillRemoveItem (event) -> events.push(event)
pane.destroyItem(item2)
expect(events).toEqual [{item: item2, index: 1, destroyed: true}]
it "invokes ::onDidRemoveItem() observers", ->
events = []
pane.onDidRemoveItem (event) -> events.push(event)
@ -496,6 +502,13 @@ describe "Pane", ->
expect(pane1.getItems()).toEqual [item1, item3]
expect(pane2.getItems()).toEqual [item4, item2, item5]
it "invokes ::onWillRemoveItem() observers", ->
events = []
pane1.onWillRemoveItem (event) -> events.push(event)
pane1.moveItemToPane(item2, pane2, 1)
expect(events).toEqual [{item: item2, index: 1, destroyed: false}]
it "invokes ::onDidRemoveItem() observers", ->
events = []
pane1.onDidRemoveItem (event) -> events.push(event)

View File

@ -163,6 +163,15 @@ class Pane extends Model
onDidRemoveItem: (callback) ->
@emitter.on 'did-remove-item', callback
# Public: Invoke the given callback before an item is removed from the pane.
#
# * `callback` {Function} to be called with when items are removed.
# * `event` {Object} with the following keys:
# * `item` The pane item to be removed.
# * `index` {Number} indicating where the item is located.
onWillRemoveItem: (callback) ->
@emitter.on 'will-remove-item', callback
# Public: Invoke the given callback when an item is moved within the pane.
#
# * `callback` {Function} to be called with when items are moved.
@ -358,6 +367,8 @@ class Pane extends Model
index = @items.indexOf(item)
return if index is -1
@emitter.emit 'will-remove-item', {item, index, destroyed}
if Grim.includeDeprecatedAPIs and typeof item.on is 'function'
@unsubscribe item
@unsubscribeFromItem(item)