Permalink
Browse files

Resize ScrollView when needed

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...
1 parent f6d0f3b commit 724055f6d5698dfc28addd30dc2a1584adbeeddf @trabianmatt trabianmatt committed Feb 18, 2013
Showing with 64 additions and 2 deletions.
  1. +18 −0 src/tb/views/collection.coffee
  2. +5 −0 test/helpers/index.coffee
  3. +41 −2 test/tb/views/collection_test.coffee
@@ -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()
@@ -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
@@ -1,6 +1,7 @@
helpers = require '../../helpers'
{ assert } = helpers.chai
+sinon = helpers.sinon
_ = require 'underscore'
@@ -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.