forked from doggy8088/ng-kp-api-sample
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
109 lines (99 loc) · 2.62 KB
/
app.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* @jsx React.DOM
*/
var API= {
KEY:"kp53f77ea99833c2.17206777",
SERVER:"http://api.kptaipei.tw/v1/",
getCategory: function(id){
id = id?id:'';
return $.get(this.SERVER+"category/"+id+"?accessToken="+this.KEY)
}
};
var Article = React.createClass({
getInitialState: function(){
return {
articles: []
}
},
getArticle: function(idx){
return this.state.articles[idx] || null;
},
componentDidMount: function(){
var _this = this;
var id = this.props.id;
API.getCategory(id).done( function(response){
_this.setState({articles: response.data});
if( !content.state.title){
content.setState(response.data[0]);
}
});
},
clickOnArticle: function(event){
$target = $(event.target);
var idx = $target.data('idx');
var article = ( this.getArticle( idx));
content.setState( article );
},
render: function(){
var _this = this;
var items = {};
this.state.articles.forEach( function(item,idx){
var id = item.id;
items['article_'+id] = <li className="article" data-idx={idx} onClick={_this.clickOnArticle}>{item.title}</li>;
});
return <ul className='articles'>{items}</ul>;
}
});
var Category = React.createClass({
getInitialState: function(){
return {
categories: []
}
},
componentDidMount: function(){
var _this = this;
API.getCategory().done( function(response){
_this.setState({categories: response.data});
});
},
render: function(){
var items = {};
this.state.categories.forEach( function(item){
var id = item.id
items['category_'+id] = <li className="category" >{item.name}<Article id={id} /></li>;
});
return <ul className='categories'>{items}</ul>;
}
});
var Loading = React.createClass({
mixin: [React.addons.PureRenderMixin],
render: function(){
if( this.props.show)
return <div className="spinner">
<div className="rect1"></div>
<div className="rect2"></div>
<div className="rect3"></div>
<div className="rect4"></div>
<div className="rect5"></div>
</div>;
else
return <div/>
}
});
var Content = React.createClass({
getInitialState: function(){
return {
title: '',
content: ''
}
},
render: function(){
return <div>
<h2>{this.state.title}</h2>
<Loading show={!this.state.title}/>
<div dangerouslySetInnerHTML={{__html: this.state.content}}></div>
</div>;
}
});
React.renderComponent( <Category />, document.getElementById('categoryContainer'));
var content = React.renderComponent( <Content />, document.getElementById('content'));