简体   繁体   English

使用来自 api 的数据创建填充复杂对象的最佳方法是什么?

[英]What is the best way to create an populate complex objects with data from api?

I am trying to build a simple todo app.我正在尝试构建一个简单的待办事项应用程序。 I have a server running on flask that returns a page when main route is accessed and has different endpoints like add_task, delete_task etc. and I access these endpoint via fetch() function and return an updated list of task from them in JSON. I have a server running on flask that returns a page when main route is accessed and has different endpoints like add_task, delete_task etc. and I access these endpoint via fetch() function and return an updated list of task from them in JSON.

My question is - What is the best way to create and populate a task list structure with following example markdown (below) with received info?我的问题是-使用以下示例 markdown(下)和收到的信息创建和填充任务列表结构的最佳方法是什么?

Markdown example: Markdown 示例:

<div class="task-list-container">
    <div class="task-container">
        <div class="task-header">
            <span>completion progress...</span>
            <span>created at ...</span>
            <span>due date...</span>
        </div>
        <div class="task-description">
            description...
        </div>
        <div class='task-comments'>task comments...</div>
        <div class='task-tags'>tags...</div>
    </div>
    ...another task
</div>

Returned JSON structure:返回 JSON 结构:

[
    {
        "task-id": int,
        "completion": bool,
        "created": date,
        "due date": date,
        "description": str,
        "comments": [str],
        "tags": [str]
    },
    {another task}
]

I could easily do it with React by creating component for it and simply populate a list of them by mapping through returned data but is there a way to do it neatly with vanilla JS?我可以通过为它创建组件来轻松地使用 React 完成它,并通过映射返回的数据来简单地填充它们的列表,但是有没有办法用 vanilla JS 巧妙地做到这一点?

You can loop through the tasks and for each task create the elements to the DOM, Something like the below:您可以遍历任务并为每个任务创建 DOM 的元素,如下所示:


Object.keys(data).forEach((item, i) => {     
       let taskItem = document.createElement('div');
       let taskItemHeader = document.createElement('div');
       let taskItemDescription = document.createElement('div');
        ....
       taskItemDescription.innerHTML = item.description;
       taskItemHeader.appendChild(taskItemDescription);
       taskItem.appendChild(taskItemHeader);
       ....
     });

There are several ways to do it.有几种方法可以做到这一点。 One is to use a template literal to inject the variable task fields.一种是使用模板文字来注入可变任务字段。 For the asynchronous part (reading from the API), you would use async and await .对于异步部分(从 API 读取),您将使用asyncawait

Here is how it could work for a dummy JSON provider:以下是它对虚拟 JSON 提供程序的工作方式:

 function renderTask(task) { return `<div class="task-container"> <div class="task-header"> <div>Task id: ${task.id}</div> <div>Task is ${task.completed? "completed": "in progress"}</div> </div> <div class="task-description"> ${task.title} </div> </div>`; } async function refresh() { let response = await fetch("https://jsonplaceholder.typicode.com/todos/"); let arr = await response.json(); let html = arr.map(renderTask).join("\n"); document.querySelector(".task-list-container").innerHTML = html; } refresh();
 .task-container { border: 1px solid; margin-top: 5px }.task-header { font-weight: bold }
 <div class="task-list-container"> </div>

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

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