Created
December 12, 2017 13:23
-
-
Save back2dos/3a72311e686fcbeea31297ab51ecfcf7 to your computer and use it in GitHub Desktop.
Infinite coconut scroller.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ; | |
import js.Browser.*; | |
import vdom.VDom.*; | |
import coconut.Ui.hxx; | |
import coconut.ui.*; | |
import coconut.data.*; | |
using Scrolling; | |
class Scrolling { | |
static function main() { | |
document.body.appendChild( | |
hxx(' | |
<InfiniteScroller data={[for (i in 0...10000) { id: i, name: "user"+i}]}> | |
<renderItem> | |
<div key={id}> | |
{name} | |
</div> | |
</renderItem> | |
</InfiniteScroller> | |
').toElement() | |
); | |
} | |
} | |
class InfiniteScroller<T> extends View<{ data:List<T>, renderItem:T->RenderResult }> { | |
@:state var limit:Int = 200; | |
function render() ' | |
<div class="scroller"> | |
<for {item in data.limit(limit)}> | |
{renderItem(item)} | |
</for> | |
<if {limit < data.length}> | |
<button onclick={limit+=200}>Show More</button> | |
</if> | |
</div> | |
'; | |
} | |
class ListTools { | |
static public function limit<T>(l:List<T>, count:Int) { | |
var pos = 0; | |
return l.filter(function (_) return if (pos++ >= count) IncludeAndStop else Include); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment