davidolrik
davidolrik

Reputation: 452

Is there a cleaner way to return just the first element from find?

The following code will return a DS.AdapterPopulatedRecordArray, but as the slug is unique in my model I just want it to return the first object.

import Ember from 'ember';

export default Ember.Route.extend({
    model: function(params) {
        return this.store.find('foo', { slug: params.slug });
    }
});

To get at the first object in my template I write model.firstObject.slug.

Is there a way to just return the first object from the Route instead of an array with one object? - and thereby only having to write model.slug in the template.

Upvotes: 1

Views: 430

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

Promises give you ability to chain additional promises to them, and the last/deepest result will be the result that's used by the model hook.

 return this.store.find('foo', { slug: params.slug }).then(function(results){
   return results.get('firstObject');
 });

Upvotes: 2

Related Questions