I frequently use the following code snippet in my projects:
{{#each model.posts as |post|}}
<div>post.title</div>
{{else}}
<div>Loading the posts...</div>
{{/each}}
However, I sometimes face uncertainty regarding whether the model.posts
array is empty or not.
How can I display a message instead of endlessly loading an empty array?
The issue arises on the /category/2
page, specifically within the category.hbs
template where the posts are "sideloaded" in the response structure, like so:
{
"data": {
"id": "1",
"type": "categories",
"attributes": {
"name": "Books"
},
"relationships": {
"posts": {
"data": [{
"id": "14",
"type": "posts"
}, {
"id": "15",
"type": "posts"
}, {
"id": "20",
"type": "posts"
}]
}
}
},
"included": [{
"id": "14",
"type": "posts",
"attributes": {
"commented": true,
"comments": 10
}
}, {
"id": "15",
"type": "posts",
"attributes": {
"commented": false,
"comments": 10
}
}, {
"id": "20",
"type": "posts",
"attributes": {
"commented": false,
"comments": 10
}
}]
}
I am utilizing ember-data and defining the following models:
category
name: DS.attr('string'),
posts: DS.hasMany('post')
post
commented: DS.attr('string'),
comments: DS.attr('number'),
category: DS.belongsTo('category')
I may consider creating an ember-twiddle to better demonstrate my issue with ember-data...