Search code examples
ember.jsember-data

Ember: How can I tell if peekAll returns any models?


I have an external API that I wish to call ONLY if I do not already have a model for the data that comes back.

I'm trying to do something like this in my route:

let positions = this.store.peekAll('position');
if (positions.content.length < 1) {
    //make API call to fetch new data
}

But I'm learning the hard way that one can't call length on .content. :/ What's the best way to get around this? I tried just looking at the variable positions, but that object is returned whether there is data or not.

PS. I can't use some of ember's built in caching methods here because I'm querying rather than using findRecord or findAll.


Solution

  • Nowadays the content sub-property should never be used, it's deprecated for public use for a couple of years now (because the content prop returns an array with InternalModels).

    You got two options:

    1. Get the native array: positions.getArray().length
    2. Get the length by it's computed property: positions.get('length')