本站PHP后端接口:  http://code.liuxinxiu.com/php/Interface/Jsoncallback.php?GUID=1&&Jsoncallback=1

 本实例用 ES6 + JSX 语法编写[对应实例] : http://liuxinxiu.com/React_Ajax_setState_Render_UI_ES6/

JavaScript代码
  1. //import React, { Component, PropTypes } from 'react'  
  2. //import $ from 'n-zepto'  
  3. var React=require('react'); //Es-5
  4. var $=require('n-zepto');   //Es-5
  5.   
  6. //import * as from './commentList.js'  
  7. //import list from './commentList.js';  
  8.   
  9.   
  10. /****** 外部子组件map数组当做模板之用 ******/  
  11. var MoviesList = React.createClass({  
  12.     render: function () {  
  13.         // this.props 用于从组件外部传入数据  
  14.         ar _movies = this.props._movies;  
  15.         return (  
  16.             <li className="datali">
  17.                 {_movies.id}-{_movies.name}
  18.             </li>  
  19.         )  
  20.     }
  21. });  
  22.   
  23. /****** 本地模拟Array数据******/  
  24. var movies = [  
  25.     {  
  26.         id: 1,  
  27.         name: '速度与激情001',  
  28.         date: 2011  
  29.     },  
  30.     {  
  31.         id: 2,  
  32.         name: '速度与激情002',  
  33.         date: 2009  
  34.     }  
  35. ];  
  36.   
  37.   
  38. /****** 使用Es5语法 React.createClass 创建一个组件 ******/  
  39. var DemoComponent = React.createClass({  
  40.     /****** 使用 getInitialState 的返回值作为数据的默认值 ******/  
  41.     getInitialState: function () {  
  42.     // this.state 用于存储数据comments被Ajax赋值要先定义个名称  
  43.         return {  
  44.             loading: true,  
  45.             title: '我喜欢的电影',  
  46.             movies: [],  
  47.         comments: []  
  48.         }  
  49.     },  
  50.   
  51.     componentDidMount: function() {  
  52.     $.ajax({  
  53.         type: 'GET',  
  54.         url: 'http://code.liuxinxiu.com/php/Interface/Jsoncallback.php',  
  55.         data: {GUID:'1'},  
  56.         dataType:'jsonp', //告诉Ajax调用$jsonp  
  57.         jsonp: "Jsoncallback", //zpeto-1.2支持自定义回调名  
  58.         success: function(data){  
  59.             var yy=JSON.stringify(data);  
  60.             var tlist=JSON.stringify(data.enttityList);  
  61.             console.log(tlist);  
  62.             //console.log(this.state.title+'22')  
  63.             this.setState({  
  64.                 comments:data.enttityList,  
  65.                 lottery:data.enttityList[0].lottery  
  66.             });  
  67.             var slist=JSON.stringify(this.state.comments);  
  68.             console.log(slist)  
  69.             //console.log(this.state.comments+'33')  
  70.         }.bind(this),  
  71.         error: function(xhr, type){  
  72.             alert(xhr+type+'Ajax error!'); //error
  73.         }  
  74.     });  
  75.     //console.log(this.state.comments+'++++--!!'); //ajax底下拿不到结果因为提前于异步  
  76.     },  
  77.   
  78.     render: function () {  
  79.         // this.state 用于存储当前的数据  
  80.         var comments = this.state.comments;  
  81.   
  82.         //var dataHtml=movies.map(<p>{name}</p>);// 注意这里 bind(this) 修正了上下文  
  83.   
  84.     /****** Movies子模板 ******/  
  85.         function renderMovies({id,name}){  
  86.             return <li>{id}-{name}</li>;  
  87.         }  
  88.     /****** Movies大模板[内嵌子模板] ******/  
  89.         //var dataHtml = movies.map(renderMovies);  
  90.   
  91.     /****** Movies大模板[外调子模板] ******/  
  92.         var dataHtml = movies.map(function(movies){  
  93.             return (  
  94.                 <MoviesList _movies={movies}/>  
  95.             )  
  96.     }.bind(this));// 注意这里 bind(this) 修正了上下文  
  97.     //console.log('movies:'+movies+'--'+'dataHtml:'+dataHtml);  
  98.   
  99.   
  100.     /****** Ajax子模板******/  
  101.         function renderComment({plat,type,name,guid,cre_time,lottery}){  
  102.             return <li>{plat}--{type}--{name}--{guid}--{cre_time}--{lottery}</li>;  
  103.         }  
  104.     /****** Ajax大模板 ******/  
  105.         var AjaxHtml = this.state.comments.map(renderComment);  
  106.     //var AjaxHtml='11'  
  107.     console.log('comments:'+comments+'--'+'AjaxHtml:'+AjaxHtml);  
  108.   
  109.     return (  
  110.         <ul>{AjaxHtml}</ul>  
  111.     )  
  112.     }  
  113. });  
  114.   
  115. /****** ES5输出给router ******/  
  116. module.exports=DemoComponent;  
  117.   
  118. /****** ES6输出给router ******/  
  119. //export default DemoComponent;  

 

H5/JS/CSS | 评论(0) | 引用(0) | 阅读(4873)