Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
Resize ScrollView when needed
Browse files Browse the repository at this point in the history
Occasionally Titanium's ScrollView needs to be reminded that it should
have a height of Ti.UI.SIZE, particularly when new views are added to a
child of the ScrollView that change its height. This commit will handle
the case where this happens within a CollectionView.
  • Loading branch information
trabianmatt committed Feb 18, 2013
1 parent f6d0f3b commit 724055f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/tb/views/collection.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,21 @@ module.exports = class CollectionView extends Chaplin.CollectionView

delegateNewEvents: (events) ->
@_delegateEvents events

# If the collection view is within a ScrollView then the ScrollView needs to
# have its height reset after items are added. If there isn't a ScrollView
# then this will be a noop.
resizeScrollView: ->

@findScrollView ?= _.once =>
@_scrollView = @$list.parents 'ScrollView'

@findScrollView().attr height: Ti.UI.SIZE

itemAdded: ->
super
@resizeScrollView()

itemsResetted: ->
super
@resizeScrollView()
5 changes: 5 additions & 0 deletions test/helpers/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ _ = require 'underscore'

app.stitchRequire('tb').load()

# Allows sinon.js to work without modification
document.createElement = ->

module.exports =

Backbone: app.Backbone

chai: require 'chai'

sinon: require 'sinon'

require: (path) ->
app.stitchRequire path
43 changes: 41 additions & 2 deletions test/tb/views/collection_test.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
helpers = require '../../helpers'

{ assert } = helpers.chai
sinon = helpers.sinon

_ = require 'underscore'

Expand Down Expand Up @@ -43,6 +44,44 @@ describe 'Collection view', ->

assert.equal table.$el.find('TableViewRow').length, 2

# assert.equal (new ViewWithoutAutoRender).$el.find('Label').length, 0
it 'should resize a ScrollView if present when adding items', ->

# assert.equal (new ViewWithAutoRender).$el.find('Label').length, 1
class Row extends Chaplin.View

tagName: 'TableViewRow'

className: -> @model.get 'name'

render: ->
@$el.html '<Label>'
@

class Table extends CollectionView

tagName: 'TableView'

autoRender: true

itemView: Row

class ScrollView extends Chaplin.View

tagName: 'ScrollView'

collection = new Backbone.Collection()

table = new Table { collection }

scrollView = new ScrollView

spy = sinon.spy();

scrollView.$el.append table.el

scrollView.el.applyProperties = spy

collection.add
id: _.uniqueId 'row'
name: 'test-3'

assert spy.called

0 comments on commit 724055f

Please sign in to comment.