mardi 8 février 2022

Emberjs - How to map api json response to a model?

While trying to build an Ember.js sample application, to call a REST Api and display results. I was able to call, fetch and display the results. In the component I am reading through the json response results.

How can I map the response data to a pre-defined model and use that in the component instead? I could not find relevant docs at https://guides.emberjs.com/release/models/defining-models/ or https://guides.emberjs.com/release/in-depth-topics/making-api-requests/#toc_where-to-make-api-requests

Code details:

  1. Using this sample API https://reqres.in/api/users
  2. Route:
export default class UsersRoute extends Route {
  async model() {
    let response = await fetch('https://reqres.in/api/users');
    let data = await response.json();
    return data.data;
  }
}
  1. Template:
<UserList
  @title="List of Users"
  @users=
/>
  1. Component:

    <tr>
      <td></td>
       <td></td>
       <td></td>
       <td></td>
    </tr>
  



Aucun commentaire:

Enregistrer un commentaire