Possible to get a computed property from DS.Snapshot?

I am using the most up to date stable versions Ember/Ember Data (2.3.1)

Is it possible to get computed property values out of an Ember Object that is exposed through the DS.Snapshot Object? I do not see an easy way to get these values, and right now I am duplicating a lot of logic to extract these values.

For example, if I have an Ember Object that looks like:

var FooModel = DS.Model.extend({
  firstName: DS.attr('string'),
  lastName: DS.attr('string'),
  fullName: function() {
    return `${this.get('firstName') ${this.get('lastName')};
  }.property('firstName', 'lastName')
});

Through the DS.Snapshot object that is exposed, I am able to get firstName and lastName through the snapshots attr method, but I do not see a way to get the fullName computed property. I have googled this and haven’t had much luck. Any insight would be helpful, thanks!

Can you explain what your use case is and where you’re accessing the snapshot? I think the answer to your basic question is that it’s not really possible — but, maybe there’s a different way to handle it given your use case!?

Sure, I can elaborate. We have a very non-standard api that can not be changed due to various reasons. As a result, I have to do a lot of data transformations in both Ember Data Adapters and Serializers.

This question was in regards to using various Adapter method hooks, like updateRecord and deleteRecord. Lots of custom headers go into these ajax calls. The only other way I see of handling this, is to create blank attributes for the given properties I want, and then using an observer function to set those properties, which probably the right way to do this. But just wanted to check before refactoring my existing adapters.

Hey Baumer, did you try with snapshot.record.get('fullName') ?

1 Like

I was in a similar situation today and can verify that this works. Thanks Frank!

1 Like

I also am confirming that @emberigniter’s solution works! Thanks Frank, love your site by the way!

1 Like