dimanche 24 décembre 2017

Ember-data: Create a record if one does not exist?

I have a computed property where I'm attempting to create a record if one does not exist, but keep getting a jQuery.Deferred exception when attempting to render the computed property.

Here's what I have so far:

deadlineDay: computed(function() {
  const oneWeekFromNow = moment().startOf('day').add(1, 'week');
  const store = this.get('store');

  return store.queryRecord('deadline-day', {
    filter: {
      date: oneWeekFromNow
    }
  }).then(result => {
    if (!result) {
      result = store.createRecord('deadline-day', {
        date: oneWeekFromNow
      });

      result.save();
    }

    return result;
  });
}),

Then in my templates I'm attempting to render with a simple helper:



The helper just calls return date.format('dddd, MMM Do')

It looks like Ember ignores the .then() block, instead attempting to render the first promise, which results in null being passed to the template.

This results in an error since .date is not a property of null.

I imagine this is an extremely common use-case, but that I have a lapse in understanding. Much help appreciated!

I'm not sure if it is relevant, but my backing store is sessionStorage via ember-local-storage




Aucun commentaire:

Enregistrer un commentaire