1
0
mirror of https://github.com/devfake/flox.git synced 2024-11-15 06:32:34 +01:00

simple lazy loading

This commit is contained in:
devfake 2015-07-30 14:07:13 +02:00
parent d2fd020911
commit 2b062cee7c
4 changed files with 58 additions and 19 deletions

View File

@ -7,7 +7,9 @@ class Category extends React.Component {
state = { state = {
category: {}, category: {},
items: [], items: [],
currentLoaded: config.loadingItems currentLoaded: config.loadingItems,
moreLoaded: false,
moreToLoad: true
} }
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
@ -27,11 +29,24 @@ class Category extends React.Component {
// todo: fix icon load. // todo: fix icon load.
render() { render() {
let loadContent;
if( ! this.state.moreLoaded) {
loadContent = <div className="load-more" onClick={this.loadMore.bind(this)}>Load more {this.state.category.name}</div>
} else {
loadContent = <i className="icon-load-more"></i>
}
return ( return (
<div> <div>
{ ! this.state.items.length ? <i className="icon-content-load"></i> : <Box items={this.state.items} category={this.state.category} type="category" currentLoaded={this.state.currentLoaded} />} {this.state.items.length
{this.state.items.length >= config.loadingItems ? <div className="wrap"><div className="load-more" onClick={this.loadMore.bind(this)}>Load more {this.state.category.name}</div></div> : ''} ? <Box items={this.state.items} category={this.state.category} type="category" currentLoaded={this.state.currentLoaded} />
: <i className="icon-content-load"></i>}
{this.state.items.length >= config.loadingItems && this.state.moreToLoad
? <div className="wrap"><div className="load-more-wrap">{loadContent}</div></div>
: ''}
</div> </div>
); );
@ -49,12 +64,22 @@ class Category extends React.Component {
} }
loadMore() { loadMore() {
Api.moreCategoryItems(this.state.category, this.state.currentLoaded).then((value) => { this.setState({
this.setState({ moreLoaded: true
currentLoaded: this.state.currentLoaded + config.loadingItems,
items: this.state.items.concat(value)
});
}); });
setTimeout(() => {
Api.moreCategoryItems(this.state.category, this.state.currentLoaded).then((value) => {
this.setState({
currentLoaded: this.state.currentLoaded + config.loadingItems,
items: this.state.items.concat(value),
moreLoaded: false,
moreToLoad: value.length ? true : false
});
});
}, 400);
} }
} }

View File

@ -70,7 +70,8 @@
} }
.icon-content-load, .icon-content-load,
.icon-box-load { .icon-box-load,
.icon-load-more {
background: url(../img/content-load.gif) no-repeats; background: url(../img/content-load.gif) no-repeats;
width: 32px; width: 32px;
height: 32px; height: 32px;
@ -88,6 +89,17 @@
top: 280px; top: 280px;
} }
.load-more-wrap {
position: relative;
float: left;
width: 100%;
height: 70px;
}
.icon-load-more {
top: 0;
}
.show-item-wrap { .show-item-wrap {
float: left; float: left;
width: 100%; width: 100%;
@ -114,11 +126,11 @@
padding: 10px; padding: 10px;
float: left; float: left;
width: 100%; width: 100%;
margin: 0 0 40px 0;
text-align: center; text-align: center;
text-transform: uppercase; text-transform: uppercase;
color: #555; color: #555;
cursor: pointer; cursor: pointer;
position: relative;
.light-theme & { .light-theme & {
background: #e9e9e9; background: #e9e9e9;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long