app.jsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /** @jsx React.DOM */
  2. $.afui.ready(function(){
  3. var _todoList=[];
  4. var todo = React.createClass({
  5. render: function() {
  6. return (
  7. <li>{this.props.name}</li>
  8. );
  9. },
  10. componentDidMount:function(){
  11. $(this.getDOMNode()).on("longTap",function(e){
  12. var cf=confirm("Are you sure you want to delete this item?")
  13. if(!cf) return;
  14. var item=e.target;
  15. React.unmountComponentAtNode(item);
  16. $(item).remove();
  17. _todoList.splice(_todoList.indexOf(item.innerHTML));
  18. });
  19. },
  20. componentWillUnmount:function(){
  21. console.log("remove test");
  22. }
  23. });
  24. var todolist = React.createClass({
  25. render: function() {
  26. var items=_todoList.map(function(item){
  27. return (
  28. <todo name={item}/>
  29. );
  30. });
  31. return (
  32. <ul className="list inset">{items}</ul>
  33. );
  34. }
  35. });
  36. /** @jsx React.DOM */
  37. var todoform = React.createClass({
  38. handleSubmit: function(e) {
  39. e.preventDefault();
  40. var val=this.refs.todoVal.getDOMNode().value.trim();
  41. if(val.length<2)
  42. return;
  43. _todoList.push(val);
  44. this.refs.todoVal.getDOMNode().value=null;
  45. renderTodos();
  46. return;
  47. },
  48. render: function() {
  49. return (
  50. <div><input ref="todoVal" type='text' placeholder='Enter Todo' />
  51. <a className='button addBtn' onClick={this.handleSubmit}>Add</a></div>
  52. );
  53. }
  54. });
  55. React.renderComponent(
  56. <todoform />,
  57. document.getElementById('todoform')
  58. );
  59. function renderTodos(){
  60. React.renderComponent(
  61. <todolist />,
  62. document.getElementById('todolist')
  63. );
  64. }
  65. renderTodos();
  66. });