2016-12-04 38 views
1

我正在做一个简单的待办事项应用程序对应用程序工作正常时,我将数据存储在预定义的对象。 但现在我是从用ajax的链接(休息)让我的数据,让问题与此, 路径 - 页/ todo.js如何等待ajax响应,然后定义对象

import React from "react"; 
    import Todo from "../components/Todo"; 
    import * as TodoActions from "../actions/TodoActions.js"; 
    import TodoStore from "../stores/TodoStore"; 

export default class Settings extends React.Component { 
    constructor(){ 
    super(); 
    this.getTodos=this.getTodos.bind(this); 
    this.state={ 
     todos: TodoStore.getAll(), 
    }; 
    console.log("STATE",this.state.todos); 
    } 
    componentDidMount(){ 
    TodoStore.on("load",this.getTodos); 
    } 

getTodos() 
{ 
    this.setState({ 
    todos:TodoStore.getAll(), 
    }); 
} 
    reloadTodos(){ 
    TodoActions.reloadTodos(); 
    } 
    render() { 
    const {todos}=this.state; 
    const TodoComponents=todos.map((todo)=>{ 
     return <Todo key={todo.id} {...todo}/>; 
    }); 
     return (
     <div> 
     <button onClick={this.reloadTodos.bind(this)}>Reload!!</button> 
     <h1>TODO.net</h1> 
     <ul>{TodoComponents}</ul> 
     </div> 
     ) 
    } 
} 

路径-stores /藤

import {EventEmitter} from "events"; 
import * as $ from "jquery"; 

import dispatcher from "../Dispatcher"; 
class TodoStore extends EventEmitter 
{ 
    constructor(){ 
    super() 
    this.todos=[]; 
    } 
    createTodo(text) 
    { const id=Date.now(); 
    this.todos.push({ 
     id, 
     text, 
     complete:false 
    }); 
    this.emit("change"); 
    } 
    getAll(){ 
     $.ajax({ 
      type: 'GET', 
      url: 'http://rest.learncode.academy/api/chitranks/todo', 
      success: function(data) { 
      console.log("here",data); 
      this.todos=data; 
      window.todos=this.todos; 
      }.bind(this), 
      error: function() { 
       alert('error GET connecting to REST'); 
      }.bind(this) 
     }); 

    return this.todos; 
    } 
    handleActions(action) { 
     switch(action.type){ 
     case "CREATE_TODO":{ 
      this.createTodo(action.text); 
     } 
     case "RECEIVED_TODOS":{ 
      this.todos=action.todos; 
      this.emit("change"); 
     } 
     } 

    } 
} 
const todoStore=new TodoStore; 
dispatcher.register(todoStore.handleActions.bind(todoStore)); 
window.dispatcher=dispatcher; 
export default todoStore; 

时我在控制台待办事项I型可以看到的数据,但它不呈现(显示未定义)中的页面

并且还/ todo.js文件的对象是未定义

+0

您可以在JavaScript中使用的回调函数,使其以 – Kebeng

+2

您不能从异步立即检索数据执行函数,因为它需要时间来接收响应请参阅[如何从异步函数检索数据](http://stackoverflow.com/q/14220321/2902660) – Pineda

回答

0

由于AJAXasynchronous您不能从response返回实际数据。请使用callbacks从响应中获取数据。

这里是更新的代码

import React from "react"; 
import Todo from "../components/Todo"; 
import * as TodoActions from "../actions/TodoActions.js"; 
import TodoStore from "../stores/TodoStore"; 

export default class Settings extends React.Component { 
    constructor(){ 
    super(); 
    this.getTodos=this.getTodos.bind(this); 
    this.state={ 
     todos: [] 
    }; 
    console.log("STATE",this.state.todos); 
    } 
    componentWillMount(){ 
    this.getTodos();//directly hydrating store with todos. 
    } 
    getTodos() 
    { 
    var _this = this; //context saved to another variable to use this in anonymous function as callback 
    TodoStore.getAll(function(todos){ 
     _this.setState({ 
     todos: todos, 
     }, function(){ console.log("updated TODOS::", _this.state.todos)}); 
    }); 
    } 
    reloadTodos(){ 
    TodoActions.reloadTodos(); 
    } 
    render() { 
    const {todos}=this.state; 
    const TodoComponents=todos.map((todo)=>{ 
     return <Todo key={todo.id} {...todo}/>; 
    }); 
    return (
     <div> 
     <button onClick={this.reloadTodos.bind(this)}>Reload!!</button> 
     <h1>TODO.net</h1> 
     <ul>{TodoComponents}</ul> 
     </div> 
    ) 
    } 
} 

和店铺

import {EventEmitter} from "events"; 
import * as $ from "jquery"; 

import dispatcher from "../Dispatcher"; 
class TodoStore extends EventEmitter{ 
    constructor(){ 
    super() 
    this.todos=[]; 
    } 
    createTodo(text){ 
    const id=Date.now(); 
     this.todos.push({ 
     id, 
     text, 
     complete:false 
     }); 
     this.emit("change"); 
    } 
    getAll(callback){ //call this on componentWillMount() (required) 
     $.ajax({ 
      type: 'GET', 
      url: 'http://rest.learncode.academy/api/chitranks/todo', 
      success: function(data) { 
      console.log("here",data); 
      this.todos=data; //store gets hydrated with todos 
      if(typeof callback == "function"){ 
     callback(data) //giving data to callback 
      } 
      window.todos=this.todos; //not necessary other than for checking in console 
      }.bind(this), 
      error: function() { 
       alert('error GET connecting to REST'); 
      }.bind(this) 
     }); 
    return this.todos; //not the proper palce to retutn 
    } 
    handleActions(action) { 
     switch(action.type){ 
     case "CREATE_TODO":{ 
      this.createTodo(action.text); 
     } 
     case "RECEIVED_TODOS":{ 
      this.todos=action.todos; 
      this.emit("change"); 
     } 
     } 

    } 
} 
const todoStore=new TodoStore; 
dispatcher.register(todoStore.handleActions.bind(todoStore)); 
window.dispatcher=dispatcher; 
export default todoStore; 
+0

这没有奏效,仍然是同样的问题,当使用异步:错误获取警告浏览器。 – chitrank

+0

我更新了你的“组件”代码。不要使用'async:false'这意味着你没有利用异步执行的力量。 –

+0

在concole中,它表示无法读取未定义的房产地图 – chitrank