简体   繁体   English

如何使用 JavaScript 更改我的待办事项应用程序的顺序?

[英]How do I change the sequence on my To Do app with JavaScript?

I'm working on a To Do App with JavaScript and I want to make a HttpRequest.我正在使用 JavaScript 开发待办事项应用程序,并且我想创建一个 HttpRequest。 It doesn't work well because I can see the data at the very end of the console so my GET request isn't work properly.它不能很好地工作,因为我可以在控制台的最后看到数据,所以我的 GET 请求不能正常工作。 How can I switch them to have the GET request at the end of the program?如何切换它们以在程序结束时发出 GET 请求?

Here is the data response:这是数据响应:

let url = 'here is my url';
let xhr = new XMLHttpRequest();
xhr.onloadend = (event) => {
            return xhr.result;
};

I set the header thus:我这样设置 header :

setHeader() {
        xhr.setRequestHeader('Content-Type','application/json');
}

My get function looks like this:我得到的 function 看起来像这样:

get(async, header) {
    return new Promise((resolve, reject) => {
        xhr.open('GET', url, async);
        xhr.setRequestHeader(header.name, header.value);
        xhr.send();
        resolve(xhr.response);
    });
}

I must use HttpClient and JavaScript, I cannot use jQuery or anything else.我必须使用 HttpClient 和 JavaScript,我不能使用 jQuery 或其他任何东西。

Please bind getAll function in constructor OR you can use Arrow function请在构造函数中绑定 getAll function 或者您可以使用箭头 function

export class HttpClient {
  constructor(url) {
    this.url = url;
    this.xhr = new XMLHttpRequest();
    this.xhr.onloadend = event => {
      console.log("ONLOADEND", this.xhr.response);
      return this.xhr.result;
    };

    this.setHeader = this.setHeader.bind(this);
    this.get = this.get.bind(this);
    this.post = this.post.bind(this);
  }

  setHeader() {
    this.xhr.setRequestHeader("Content-Type", "application/json");
  }

  get(async, header) {
    return new Promise((resolve, reject) => {
      this.xhr.open("GET", this.url, async);
      this.xhr.setRequestHeader(header.name, header.value);
      this.xhr.send();
      console.log("xhr", this.xhr.response);
      resolve(this.xhr.response);
      // return nothing
    });
  }

  post(async, data, header) {
    return new Promise((resolve, reject) => {
      this.xhr.open("POST", this.url + "create", async);
      this.setHeader(header);
      this.xhr.send(data);
      console.log("RESPONSE", this.xhr.response);
      resolve(this.xhr.response);
    });
  }
}

export class TodoList extends HttpClient {
  constructor() {
    super("http://todoapp.test/api/");
    this.items = [];
    this.header = new Headers();
    this.header.set("Accept-Encoding", "application/json");

    this.add = this.add.bind(this);
    this.getAll = this.getAll.bind(this);
  }

  add(todo) {
    this.post(true, JSON.stringify(todo), this.header);
    this.items.push(todo);
  }

  getAll() {
    this.get(true, this.header).then(result => {
      const data = !!result ? result : "[]";
      const items = JSON.parse(data);
      console.log("result:", result);
      items.forEach(item => {
        const todo = new Todo(item.id, item.name, item.status);
        this.items.push(todo);
      });
      console.log("items block:", this.items);
    });
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM