Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Don't empty the header/footer if the given header and footer function…

…s don't return anything. This allows us to update the header and footer on view change without emptying it first
  • Loading branch information...
commit 440e7794eec5a915a9e232d81e7919cdde3a8017 1 parent a7e950b
Marcus Westin authored December 19, 2012

Showing 1 changed file with 26 additions and 17 deletions. Show diff stats Hide diff stats

  1. 43  scroller.js
43  scroller.js
... ...
@@ -1,6 +1,7 @@
1 1
 var tags = require('./tags')
2 2
 var viewport = require('./viewport')
3 3
 var div = tags('div')
  4
+var style = require('./style')
4 5
 
5 6
 module.exports = scroller
6 7
 
@@ -20,10 +21,12 @@ function scroller(opts) {
20 21
 
21 22
 var scrollerBase = {
22 23
 	headHeight:0,
23  
-	renderHead:function(headHeight, renderHeadContent) {
24  
-		this.renderHeadContent = renderHeadContent
  24
+	renderHead:function(headHeight, headFn) {
25 25
 		this.headHeight = headHeight
26  
-		return this.$head=$(div('tags-scroller-head', style({ height:headHeight, width:'100%', position:'relative', top:0, zIndex:2 })))
  26
+		this.headFn = headFn
  27
+		return this.$head=$(div('tags-scroller-head', style({
  28
+			height:headHeight, width:'100%', position:'relative', top:0, zIndex:2
  29
+		})))
27 30
 	},
28 31
 	renderBody:function(numViews, renderBodyContent) {
29 32
 		this.renderBodyContent = renderBodyContent
@@ -50,12 +53,15 @@ var scrollerBase = {
50 53
 				))
51 54
 			)
52 55
 		)
53  
-		this.push({})
  56
+		var self = this
  57
+		setTimeout(function() { self.push({}) })
54 58
 		return this.body
55 59
 	},
56  
-	renderFoot:function(renderFootContent) {
57  
-		this.renderFootContent = renderFootContent
58  
-		return this.$foot = $(div('tags-scroller-foot'), style({ width:'100%', position:'absolute', bottom:0, zIndex:2 }))
  60
+	renderFoot:function(footFn) {
  61
+		this.footFn = footFn
  62
+		return this.$foot = $(div('tags-scroller-foot'), style({
  63
+			width:'100%', position:'absolute', bottom:0, zIndex:2
  64
+		}))
59 65
 	},
60 66
 	push:function scollerPush(newView, opts) {
61 67
 		opts = tags.options(opts, {
@@ -96,23 +102,26 @@ var scrollerBase = {
96 102
 			if (this.onViewChange) {
97 103
 				this.onViewChange()
98 104
 			}
99  
-			if (this.$head) {
100  
-				this.$head.empty().append(
101  
-					this.renderHeadContent(opts.view, { viewBelow:this.views[opts.index - 1] })
102  
-				)
103  
-			}
104  
-			if (this.$foot) {
105  
-				this.$foot.empty().append(
106  
-					this.renderFootContent(opts.view, { viewBelow:this.views[opts.index - 1] })
107  
-				)
108  
-			}
  105
+			this._update(opts, this.$head, this.headFn)
  106
+			this._update(opts, this.$foot, this.footFn)
  107
+
109 108
 			var keepStaleView = (opts.useStaleView && this.views[opts.index])
110 109
 			if (!keepStaleView) {
111 110
 				this.views[opts.index].empty().append(this._renderBodyContent(opts))
112 111
 			}
  112
+			
113 113
 			this._scroll(animate)
114 114
 		}
115 115
 	},
  116
+	_update:function(opts, $element, updateFn) {
  117
+		if (!updateFn) { return }
  118
+		var fnResult = updateFn.call($element, opts.view, { viewBelow:this.views[opts.index - 1] })
  119
+		if (fnResult) {
  120
+			// Allow for the header and footer to return content to be displayed.
  121
+			// Alt. they can choose not re-render, but rather update what's already rendered and avoid a flash of no content.
  122
+			$element.empty().append(fnResult)
  123
+		}
  124
+	},
116 125
 	_renderBodyContent:function(opts) {
117 126
 		var renderOpts = { index:opts.index }
118 127
 		var alwaysBounce = (opts.alwaysBounce === null ? this.alwaysBounce : opts.alwaysBounce)

0 notes on commit 440e779

Please sign in to comment.
Something went wrong with that request. Please try again.