Quantcast
Channel: Ember.JS - Latest topics
Viewing all articles
Browse latest Browse all 4828

Testing for Record Equality in Ember Data

$
0
0

@onsmith wrote:

Hi,

While using Ember Data, I repeatedly find myself needing to test if two records are the same.

If the records are both resolved, this is no big deal; however, often one or both of the records are actually promises that came from a related model. For example:

//app/models/post.js
export default DS.Model.extend({
  subject: DS.attr('string'),
  user:  DS.belongsTo('user', { async: true }),
});

//app/models/user.js
export default DS.Model.extend({
  name: DS.attr('string'),
  posts: DS.hasMany('post', { async: true }),
});

// Code in a route, controller, or component somewhere
const currentUser = this.get('session.currentUser');
const post        = this.get('model');

if (currentUser === post.get('user')) {
  // This is never executed,
  // because post.get('user') returns a promise,
  // which can't be directly compared to a record
}

My question: is there any robust way to test for record equality that handles promise resolving? I keep finding myself using post.get('user').content to fix this problem anytime I know that the promise is already resolved. Obviously this doesn't feel right.

TL;DR: Promises make testing for record equality really clunky, at least the way I'm doing it.

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4828

Trending Articles