Use contentArrayWillChange for deleting records from LimitedArray

Using contentArrayDidChange is usless for deletion, because the records
were already deleted, so we can't fetch them from the content array.
This commit is contained in:
Piotr Sarnacki 2012-12-11 15:51:07 +01:00
parent e36fb0d7a2
commit fd82bf3791
2 changed files with 26 additions and 17 deletions

View File

@ -68,7 +68,6 @@ Travis.Store = DS.Store.extend
{ id: id, clientId: clientId } { id: id, clientId: clientId }
receive: (event, data) -> receive: (event, data) ->
console.log event, data
[name, type] = event.split(':') [name, type] = event.split(':')
mappings = @adapter.get('mappings') mappings = @adapter.get('mappings')

View File

@ -8,11 +8,11 @@ Travis.LimitedArray = Em.ArrayProxy.extend
arrangedContent: (-> arrangedContent: (->
content = @get('content') content = @get('content')
if @get('disable') if @get('disabled')
content content
else if content else if content
content.slice(0, @get('limit')) content.slice(0, @get('limit'))
).property('content', 'limit', 'disable') ).property('content', 'limit', 'disabled')
totalLength: (-> totalLength: (->
@get('content.length') @get('content.length')
@ -33,31 +33,41 @@ Travis.LimitedArray = Em.ArrayProxy.extend
showAll: -> showAll: ->
@set 'limit', 1000000000 @set 'limit', 1000000000
@set 'disable', true @set 'disabled', true
contentArrayWillChange: (array, index, removedCount, addedCount) ->
@_super.apply this, arguments
return if @get('disabled')
if removedCount
arrangedContent = @get 'arrangedContent'
removedObjects = array.slice(index, index + removedCount);
console.log 'willChange', @get('name'), index, removedCount, addedCount, arrangedContent.map( (j) -> "#{j.get('repoSlug')}-#{j.get('number')}" ), removedObjects.map( (j) -> "#{j.get('repoSlug')}-#{j.get('number')}" )
arrangedContent.removeObjects(removedObjects)
contentArrayDidChange: (array, index, removedCount, addedCount) -> contentArrayDidChange: (array, index, removedCount, addedCount) ->
@_super.apply this, arguments @_super.apply this, arguments
return if @get('disable') return if @get('disabled')
limit = @get 'limit' if addedCount
arrangedContent = @get('arrangedContent') arrangedContent = @get('arrangedContent')
length = arrangedContent.get 'length'
if addedCount > 0
addedObjects = array.slice(index, index + addedCount) addedObjects = array.slice(index, index + addedCount)
for object in addedObjects for object in addedObjects
if @get('insertAtTheBeginning') if @get 'insertAtTheBeginning'
arrangedContent.unshiftObject(object) arrangedContent.unshiftObject(object)
else else
arrangedContent.pushObject(object) arrangedContent.pushObject(object)
if removedCount @balanceArray()
removedObjects = array.slice(index, index + removedCount);
arrangedContent.removeObjects(removedObjects)
balanceArray: ->
limit = @get 'limit'
arrangedContent = @get 'arrangedContent'
length = arrangedContent.get 'length' length = arrangedContent.get 'length'
content = @get('content') content = @get 'content'
if length > limit if length > limit
arrangedContent.replace(limit, length - limit) arrangedContent.replace(limit, length - limit)
@ -66,7 +76,7 @@ Travis.LimitedArray = Em.ArrayProxy.extend
while count > 0 while count > 0
if next = content.find( (object) -> !arrangedContent.contains(object) ) if next = content.find( (object) -> !arrangedContent.contains(object) )
if @get('insertAtTheBeginning') if @get('insertAtTheBeginning')
arrangedContent.unshiftObject(object) arrangedContent.unshiftObject(next)
else else
arrangedContent.pushObject(object) arrangedContent.pushObject(next)
count -= 1 count -= 1