Ember data find record minimum value

How do I scan all the records for a given model and return the record containing the lowest value of a given item? For example, the youngest person?


I think Store - 4.6 - Ember API Documentation is what you are looking for.

I’m not sure I understand how the filter() method would help me, can you please give an example?

I realised that I did understand your question wrong. I suppose you have a list of model like ‘people’ and you want to filter out the youngest person.

people = [
  {age: 23},
  {age: 40},
  {age: 65},

peopleController = Ember.ArrayController.create({
  content: people,
  sortProperties: ['age'],
  sortAscending: true

var youngest = peopleController.objectAt(0)

Okay, that makes sense. However, that will only work if the controller is set to sort on age. However, suppose that people is sorted by name instead?

For the time being I am using the following hack:

var age_max = 0;
    var age_next = item.get('age');
    if (age_next > age_max) {
       age_max = age_next;

There must be a more elegant way of doing this.

I might be missing something, but it seems like a computed property would do nicely here. Ember already bundles one that would seem to do the trick: computed.min.

In your case, it looks like your model is a collection of records that have an age property, meaning the example in the docs is exactly what you want:

  ages: Ember.computed.mapBy('model', 'age'),
  minAge: Ember.computed.min('ages')

On an unrelated note, is this really an Ember Data question?