Skip to content
This repository was archived by the owner on Nov 25, 2020. It is now read-only.

ListViewController: extract index decision to its own method #56

Merged
merged 1 commit into from
Aug 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/components/list/listviewcontroller.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ module.exports = class KDListViewController extends KDViewController
HELPERS
###

# get the index depending on the
# conditions. e.g `options.lastToFirst`
# This method can be overriden by subclasses
# to change the item order and get necessary index.
getIndex: (index) ->
return if @getOptions().lastToFirst
then @getItemCount() - index - 1
else index

itemForId: (id) -> @itemsIndexed[id]

getItemsOrdered: -> @itemsOrdered
Expand Down Expand Up @@ -180,7 +189,7 @@ module.exports = class KDListViewController extends KDViewController
unregisterItem: (itemInstance, index) ->

@emit 'UnregisteringItem', itemInstance, index
actualIndex = if @getOptions().lastToFirst then @getItemCount() - index - 1 else index
actualIndex = @getIndex index

@getListItems().splice actualIndex, 1
if itemInstance.getData()?
Expand Down
27 changes: 27 additions & 0 deletions test/components/list/listviewcontroller.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,33 @@ describe 'KDListViewController', ->

assert viewController.emit.calledWith 'AllItemsAddedToList'

describe 'getIndex', ->

realItemCountFn = KDListViewController::getItemCount

before -> KDListViewController::getItemCount = -> 10
after -> KDListViewController::getItemCount = realItemCountFn

context 'when it is last to first', ->

it 'returns from bottom', ->
listViewStub = sinon.createStubInstance KDListView
viewController = new KDListViewController { view: listViewStub, lastToFirst: yes }

expected = viewController.getIndex 2

assert.equal expected, 7

context 'when it is not last to first', ->

it 'returns from top', ->
listViewStub = sinon.createStubInstance KDListView
viewController = new KDListViewController { view: listViewStub, lastToFirst: no }

expected = viewController.getIndex 2

assert.equal expected, 2


describe 'putNoItemView', ->

Expand Down