Check null related entity in Ember and Ember data

When the related data is serialized in a 0-1 ratio, everything works fine.

"lesson": {
  "id": 1,
  "title": "foo",
  "user_completion": {
    "id": 1,
    "percent": 30
  },
  "is_available": true
}

      

However, when the associated model has not yet been created, the web service returns null for this relationship.

"lesson": {
  "id": 1,
  "title": "foo",
  "user_completion": null,
  "is_available": false
}

      

It looks like when the ember model is created with this data, the user_complex becomes a PromiseObject .

  • Should my web service return null for this relationship if no matching one-to-one-one-one entry exists?
  • Is it correct for Ember Data to put a promise object on this model property, even though it can never resolve anything? Am I missing something important?
  • What should I do in cases where I need to check for related data, like the following example?

    model.filter(function(item) {
      return item.get('is_available') || item.get('user_completion') !== null;
    });
    
          

    Do I really need to check if user_completion is a PromiseObject? It seems a little odd and I feel like I made a mistake somewhere else.

+3


source to share


1 answer


After some more research, it turns out that if you have a relationship defined as asynchronous, it will always return a promise, whether you are entering the relationship or not.

Edit

user_completion: DS.belongsTo('user_completion', {async: true})

      

to



user_completion: DS.belongsTo('user_completion', {async: false})

      

and everything works as expected; Ember returns null

for an empty link.

For what it's worth, it looks like it async: true

could become the default in the future .

+5


source







All Articles