Skip to content
This repository was archived by the owner on Jan 19, 2024. It is now read-only.

Commit 0e3d512

Browse files
committed
Pull-to-Refresh works
1 parent bee4f41 commit 0e3d512

2 files changed

Lines changed: 52 additions & 8 deletions

File tree

src/ScrollRefresh/index.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,46 @@
2828

2929
import React, {
3030
View,
31-
ScrollView
31+
ScrollView,
32+
RefreshControl
3233
} from 'react-native';
3334

3435
module.exports = React.createClass ({
36+
contextTypes: {
37+
doRefresh: React.PropTypes.func,
38+
refreshedDate: React.PropTypes.instanceOf(Date)
39+
},
40+
41+
getInitialState(){
42+
return {
43+
refreshing: false,
44+
};
45+
},
46+
47+
onRefresh(){
48+
this.setState({refreshing:true});
49+
if(this.context.doRefresh){
50+
this.context.doRefresh();
51+
}
52+
},
53+
54+
componentWillUpdate(nextProps, nextState, nextContext){
55+
if(nextContext.refreshedDate !== this.context.refreshedDate){
56+
this.setState({refreshing:false});
57+
}
58+
},
59+
3560
render() {
3661
return (
37-
<ScrollView {... this.props} style={[this.props.style]}>
62+
<ScrollView {... this.props}
63+
style={[this.props.style]}
64+
refreshControl={
65+
<RefreshControl
66+
refreshing={this.state.refreshing}
67+
onRefresh={this.onRefresh}
68+
/>
69+
}
70+
>
3871
{this.props.children}
3972
</ScrollView>
4073
)

src/SobjContainer/index.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,26 @@ module.exports = React.createClass ({
8585
childContextTypes: {
8686
sobj: React.PropTypes.object,
8787
compactLayout: React.PropTypes.object,
88-
defaultLayout: React.PropTypes.object
88+
defaultLayout: React.PropTypes.object,
89+
doRefresh: React.PropTypes.func,
90+
refreshedDate: React.PropTypes.instanceOf(Date)
8991
},
9092
getInitialState(){
9193
return {
9294
sobj:this.props.sobj?this.props.sobj:{Name:' ',attributes:{}},
9395
compactLayout:{},
9496
defaultLayout:{},
95-
loading:false
97+
loading:false,
98+
refreshedDate: new Date()
9699
};
97100
},
98101
getChildContext() {
99102
return {
100103
sobj: this.state.sobj,
101104
compactLayout:this.state.compactLayout,
102-
defaultLayout:this.state.defaultLayout
105+
defaultLayout:this.state.defaultLayout,
106+
doRefresh:this.handleRefresh,
107+
refreshedDate:this.state.refreshedDate
103108
};
104109
},
105110
componentDidMount(){
@@ -109,12 +114,17 @@ module.exports = React.createClass ({
109114
componentWillUnmount(){
110115
unsubscribe(this);
111116
},
117+
handleRefresh(){
118+
console.log('>>> REFRESH !!!');
119+
this.getInfo(true);
120+
},
112121
updateSobj(sobj,compactLayout,defaultLayout){
113122
this.setState({
114123
sobj:sobj,
115124
loading:false,
116125
compactLayout:compactLayout,
117-
defaultLayout:defaultLayout
126+
defaultLayout:defaultLayout,
127+
refreshedDate: new Date()
118128
});
119129
},
120130
handleDataLoad(){
@@ -125,19 +135,20 @@ module.exports = React.createClass ({
125135
});
126136
}
127137
},
128-
getInfo() {
138+
getInfo(nocache) {
129139
this.setState({loading:true});
130140
if(!this.props.type || !this.props.id){
131141
return;
132142
}
133-
getByTypeAndId(this.props.type,this.props.id)
143+
getByTypeAndId(this.props.type,this.props.id,nocache)
134144
.then((opts)=>{
135145
if(opts.cachedSobj){
136146
this.setState({
137147
sobj:opts.cachedSobj,
138148
compactTitle: opts.cachedSobj.attributes.compactTitle,
139149
compactLayout:opts.cachedCompactLayout,
140150
defaultLayout:opts.cachedDefaultLayout,
151+
refreshedDate: new Date()
141152
});
142153
}
143154
});

0 commit comments

Comments
 (0)