How to get count of model type in the store

@dreamingfurther and I are trying to conditionally make a request for all models of a given type

Ideally we would like to do something like.

if (User.count === 0) {
   User.find()
}

We’ve come up with a solution using a filter to count the records but it seems hacky:

var count = 0;
User.filter(function() {
   count+=1;
});
if (count === 0) {
   User.find()
}

Does anyone have any suggestions on how to handle this?

How about this?

User.all().get("length") === 0;

Here’s an alternative approach I’m using in an app:

App.ApplicationController = Em.Controller.extend({
  init: function() {
    return App.Subject.find();
  }
});

App.SubjectsRoute = Ember.Route.extend({
  model: function() {
    return App.Subject.all();
  }
});
1 Like

Fantastic @gunn! That’s exactly what we need.