Identifying the empty population of a hasMany property

I'm facing a challenge that seems simple, but I'm struggling to find the solution.

Currently, I'm working with Emberjs 2.8.

In my model, I have an async: true property set up like this:

models/my-model.js

import DS from 'ember-data';

export default DS.Model.extend({
  childModels: DS.hasMany('otherModel', {async: true})
});

There's a component that is responsible for displaying these models:

/components/my-component.js

import Ember from 'ember';
export default Ember.Component.extend({
   theModel: // model of type my-model here
})

In some cases, the my-model.childModels may not have any records. What I want to achieve is to show a "no children found" message in the template only after the async call to the server returns an empty response. Ideally, I'd like the template structure to be like this:

templates/components/my-component.hbs

<div>
{{#if theModel.childModels.length == 0}}
   // display message saying there are no children
{{else}}
   {{#each theModel.childModels as |child}}
      // do something
   {{/each}}
{{/if}}
</div>

The complexity lies in determining whether the relationship data has been populated from the server. Checking for .length == 0 isn't sufficient as it can trigger before the async call completes, resulting in unnecessary DOM manipulation. I need to find a way to check if theModel.childModels.isLoaded, although I haven't figured out how to do it even after going through the documentation.

If anyone has suggestions on how to tackle this issue, your advice would be greatly appreciated. Thank you in advance!

Answer №1

Well, I'm feeling a bit foolish because the answer was right there in the documentation all along. The PromiseManyArray actually utilizes the PromiseProxyMixin, which does indeed have an isPending property. So, all I need to do is create a computed property like this:

  childModelsEmpty: Ember.computed('theModel.childModels', function () {
    return !this.get('theModel.childModels.isPending') && this.get('theModel.childModels.length') === 0;
  }),

This will help me determine whether the server has been contacted and returned an empty response.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing the invalid expression error to pop up in my Vue.js template?

I encountered an error within my vue single file component: Errors compiling template: invalid expression: Unexpected token { in {{ jobs[0].build_link }} Raw expression: v-bind:href="{{ jobs[0].build_link }}" The complete code causing the is ...

Managing data from two tables in Node.js with ejs

I have a question regarding my node.js project that I need help with. As a beginner in this field, I believe the answer may be simpler than anticipated. In my code file named index.js, I found the following snippet after referring to some online documenta ...

Utilizing keyboard shortcuts for webpage navigation (within a content script)

Just to clarify, I'm not looking for code assistance right now. I mostly need a starting point. My goal is to create a Chrome browser extension that enables me to use keyboard keys for navigation on a specific webpage. The page in question belongs t ...

Downloading a file utilizing Selenium through the window.open method

I am having trouble extracting data from a webpage that triggers a new window to open when a link is clicked, resulting in an immediate download of a csv file. The URL format is a challenge as it involves complex javascript functions called via the onClick ...

Verify whether a specific point in time falls within the same week as any date string within an array of date strings

I am working on my backbone-app copyCLDRView and I am trying to replicate weeks along with their components and data. In simpler terms, I want to "copy one week and all its models into another week." My goal is to check if the target week has at least one ...

Updating a table in Javascript after deleting a specific row

Is there a way to automatically reindex rows in a table after deleting a row? For example, if I delete row 1, can the remaining rows be reordered so that they are numbered sequentially? function reindexRows(tableID) { try { var t ...

The Expressjs router prioritizes resolving before retrieving data from Firestore

UPDATE: Upon further investigation, I discovered that by adding "async" to the function signature, the output now displays in the intended order. However, I am still encountering an error regarding setting headers after they have already been sent, even th ...

When utilizing Ionic Firebase, the user profile saved in the sidemenu is stored using the Events class. However, the saved profile disappears as soon as

Hello there. I am currently working on displaying my user's information in the sidemenu, and after some research, I found that using the Events class might solve this issue. However, I have noticed that the data saved in subscribe gets destroyed whene ...

looking to showcase the highest 'levelNumber' of elements within an array

arr1 = [ { "levelNumber": "2", "name": "abc", }, { "levelNumber": "3", "name": "abc" }, { "levelNumber": "3", "name": &quo ...

Guide on incorporating Vue components: implementing component 2 within the template of component 1

I'm a Vue beginner and struggling with how to use one component within the template of another or how to combine them in HTML. I've tried looking through the documentation and Stack Overflow but can't seem to figure it out. Currently, I am ...

What is the process for entering a value into mysql based on the date?

Seeking assistance with a specific coding challenge. The task at hand involves inputting a date, such as '2018-05-08', and based on the user's input, storing the value in different columns of a database table. For instance, if the date is &a ...

Developing a void or vacancy using three.js

Thinking about creating a unique shape in three.js, similar to a 3x3x3 cube with a 1x1x1 hole in it. Is there a way to use cubegeometry initially and then apply another geometry to remove the unwanted parts, like a deletion geometry? :D Appreciate any hel ...

The symbol in my Google Maps path is off-center and not aligned correctly

Hey everyone, I'm currently working on a project that involves a plane moving along a polyline, but I'm facing an issue where the path symbol is not centered as demonstrated in this image This is what I have: Here, I define the path symbol as t ...

Similar to TypeScript's `hasOwnProperty` counterpart

When working with TypeScript objects, is there a way to loop through a dictionary and set properties of another dictionary similar to how it is done in JavaScript? for (let key in dict) { if (obj.hasOwnProperty(key)) { obj[key] = dict[key]; } } If ...

Customizing Typeahead Drop Down Width in Angular Project

I've been struggling with customizing the Bootstrap UI Typeahead component. I am attempting to dynamically adjust the width of the dropdown box. The solution provided in the previous Stack Overflow question I asked worked in a different context, but f ...

When working with Next.js, I encountered a scenario where the useEffect() function was being triggered twice. I attempted to solve this issue by providing an empty array as the second argument, but to no

When working with Next-JS, I encountered an issue where the useEffect() function was running twice. While I heard that using an empty array as the second argument can fix this in React, it did not work in my case within Next-JS (which is based on React). ...

Managing "unprocessed information" in a Node.js environment and transferring the information through a Node Express endpoint

Currently, I am in the process of making an API call to retrieve a file using axios: async function fetchData() { const configuration = {}; // { responseType: 'stream'}; const { response } = await axios.get(URL, configuration); c ...

I often find myself pondering the significance of objects such as [, thisArg]

At times, I delve into JavaScript code on MDN and come across some confusing syntax like [, thisArg]... for instance, arr.map(callback(currentValue[, index[, array]])[, thisArg]) In this scenario, I am aware that a callback function is required. But what ...

"Upon returning from a child component, the React component's method 'this.state.myState'

In my React code, I have a Parent component called BookApplication and a child component called SearchBox. The SearchBox contains an input field that should pass the input back to the parent component for event handling. The data flow seems to be working c ...

Enhance Your Website with Bootstrap 5 Slider/Carousel for Product Showcase

I am attempting to create a slider/carousel of products similar to the image shown below using Bootstrap 5: https://i.sstatic.net/7FhPw.png Here is the current code I have: <style> <!-- Temporary --> .carousel-control-next-icon { bac ...