How do I map JSON data with my model?

#1

I am using Ember Data but I have problem how to map it with my model.

This is my JSON api look like.

What I want to map with my model is in the attributes field

Can you help me to guide from beginning how to fetch data from server until render it, please.

Thank you very much

0 Likes

#2

your model should look something like this:

export default class Post extends Model {
  @attr() name;
  @attr() priceFixed;
  // etc

  @belongsTo() gallery;
  @belongsTo() currency;
}

run this in the terminal to get your model class:

ember g model post

and it looks like you’re using {json:api}, so, you’ll want to make sure your adapter inherits from JSONAPIAdapter and the serializer inherits from JSONAPISerializer

This may be of interest to you: https://guides.emberjs.com/release/models/defining-models/

I don’t know how well you know ember / how new you are, so if you need more detail, feel free to ask :slight_smile:

1 Like

#3

Thank you very much Sir, Yes I am new to Ember and I can get data from API and already pass to template (hbs) files.

My problem is when I try to loop through in template file, i need to use it like this

    {{#each model as |product|}}
        <h4>Pro: {{product.attibutes.name}}</h4>
    {{/each}}

And this seem to be not the same as my model. And in my model I have to have a field call attributes and it seem not correct way to do it. What should I do?

1 Like

#4

You should be able to access ‘name’ on the product :wink:

Can you share your model?

0 Likes

#5

Yes can.

But now I found solutions. Thank you very much sir.

0 Likes

#6

If you get a chance could you post your solution? Others might find it super helpful later on :smile:

0 Likes