todo_api/examples/client_react.html
Maurits van der Schee 4cdc6aaab7 Improved client examples
2016-04-08 16:16:25 +02:00

52 lines
1.3 KiB
HTML

<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.0/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.0/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/zepto/1.1.6/zepto.min.js"></script>
<script src="../lib/php_crud_api_transform.js"></script>
<script>
'use strict';
var PostList = React.createClass({
displayName: 'PostList',
url: 'http://localhost/api.php/posts',
getInitialState: function getInitialState() {
return { posts: [] };
},
componentDidMount: function() {
$.post(this.url, {user_id:1,category_id:1,content:"from react"});
this.serverRequest = $.get(this.url, function (data) {
this.setState(php_crud_api_transform(data));
}.bind(this));
},
componentWillUnmount: function() {
this.serverRequest.abort();
},
render: function render() {
var createPost = function(post) {
return React.createElement(
'li',
{key: post.id},
post.id,
', ',
post.content
);
};
return React.createElement(
'ul',
null,
this.state.posts.map(createPost)
);
}
});
$(function(){ ReactDOM.render(React.createElement(PostList, null), document.getElementById('myApplication')); });
</script>
</head>
<body>
<div id="myApplication">Loading...</div>
</body>
</html>