-
Notifications
You must be signed in to change notification settings - Fork 8
/
TopicList.js
86 lines (75 loc) · 1.79 KB
/
TopicList.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'use strict';
var React = require('react-native');
var DetailView = require('./DetailView');
var Loading = require('./Loading');
var ListCell = require('./ListCell');
var Separator = require('./Separator');
var {
StyleSheet,
Text,
View,
ListView
} = React;
var REQUEST_LATEST_URL = 'https://www.v2ex.com/api/topics/latest.json';
var REQUEST_NODE_URL = 'https://www.v2ex.com/api/topics/show.json';
var TopicList = React.createClass({
getInitialState () {
return {
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
}),
loaded: false
};
},
componentDidMount () {
var url = REQUEST_LATEST_URL;
if (this.props.nodeId) {
url = `${ REQUEST_NODE_URL }?node_id=${ this.props.nodeId }`;
}
fetch(url)
.then((response) => response.json())
.then((responseData) => {
this.setState((state) => {
return {
dataSource: state.dataSource.cloneWithRows(responseData),
loaded: true
};
});
})
.done();
},
gotoDetail (item) {
// AlertIOS.alert('你好', '再见');
this.props.navigator.push({
title: '话题',
component: DetailView,
passProps: {item}
});
},
render () {
if (!this.state.loaded) {
return <Loading />;
}
return (
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderItem}
renderSeparator={this.renderSeparator}
/>
);
},
renderSeparator () {
return <Separator />;
},
renderItem (item) {
return (
<ListCell
key={item.id}
item={item}
gotoDetail={this.gotoDetail.bind(this, item)}
showNode={typeof this.props.nodeId === 'undefined'}
/>
);
}
});
module.exports = TopicList;