dimanche 25 septembre 2016

belongsTo relation call returns null within observer

I have belongsTo relation on a model:

//book.js
export default DS.Model.extend({
    name: attr('string'),
    item: belongsTo('item', {async: true}),
});

//item.js
export default DS.Model.extend({
    name: attr('string')
});

When I call:

let b = store.createRecord('book', {name: 'test'});

b.save().then(function(b) {
  //b is saved correctly here

  b.get('item').then(function(item) {

  //item relation is gotten successfully - all works well

 });

});

(NOTE: I'm not initially setting the item relation when creating the book record, because this is determined by the backend response)

...everything works as expected so far.

now, somewhere else I have an observer that fires when a change to a books array is made.

when it fires I'm iterating over the books array with the newly saved book and calling the relation item the exact same way as I do right after saving the book.

but for some reason item relation on the book record is always null within the observer.

booksChanged: on('init', observer('books.[]', function() {

//this gets triggered
    let b = this.get('books').get('firstObject');//->just assume the book created is the first object

   b.get('item').then(function(item) {

     //item is null, why???

   });


}));

version 2.7 ember and ember-data.

any thoughts?

thanks




Aucun commentaire:

Enregistrer un commentaire