Search code examples
ember.jsember-dataember-cli

Returning 1 object/record from my model in Ember


In a test app I running, I have fixture data for employees with name, department, etc. Currently, my model contains all of the employees, and I am able to make an employee list. However, I want to make a side component that lists the information of 1 specific employee (i.e. the one clicked, as a tooltip). How can I pass just 1 employee to my component?

I have an action that updates a property whenever someone clicks a name. I thought to do a computed property to query the model based on that name, but I am not sure how to filter my model to return just the one employee.

actions: {
  updateProfile(person) {
    set(this, 'profile', person);
  }
}

And my computed property:

currentProfile: computed('profile', function(){
  return this.model.find('person', {'name': get(this, 'profile')});
}),

Is there a simple way to return just the 1 object I want from my model?

Solution

I needed to filter on this and not the model itself. Then, I had to just return the firstObject, even though there was only 1 match (that was the main issue).

 currentProfile: computed('profile', function(){
    return this.filterBy('name', get(this, 'profile')).get('firstObject');
 }),

Solution

  • Try this:

    currentProfile: computed('profile', function(){
      var query = {'name': get(this, 'profile')};
      return this.model.find('person', query).then(function(people){
        return people.get('firstObject');
      });
    })