From d3269e3147cb7bbaea4b60d50784bca4e91932d1 Mon Sep 17 00:00:00 2001 From: Devin Weaver Date: Mon, 6 Mar 2017 06:43:06 -0500 Subject: [PATCH] Refactor admin/address/route to use e-c This is the example I used in issue #969. It is illustrates how to use ember-concurrency tasks in routes. --- app/admin/address/route.js | 42 ++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/app/admin/address/route.js b/app/admin/address/route.js index f1fe6c8dcd..41e00a1119 100644 --- a/app/admin/address/route.js +++ b/app/admin/address/route.js @@ -1,5 +1,5 @@ import AbstractEditRoute from 'hospitalrun/routes/abstract-edit-route'; -import Ember from 'ember'; +import { task } from 'ember-concurrency'; import { translationMacro as t } from 'ember-i18n'; import UnauthorizedError from 'hospitalrun/utils/unauthorized-error'; @@ -8,24 +8,26 @@ export default AbstractEditRoute.extend({ newTitle: t('admin.address.newTitle'), editTitle: t('admin.address.editTitle'), model() { - return new Ember.RSVP.Promise((resolve, reject) => { - this.get('store').find('option', 'address_options').then((addressOptions) => { - resolve(addressOptions); - }, (err) => { - if (err instanceof UnauthorizedError) { - reject(err); - } else { - let store = this.get('store'); - let newConfig = store.push(store.normalize('option', { - id: 'address_options', - value: { - address1Label: this.get('i18n').t('admin.address.addressLabel'), - address1Include: true - } - })); - resolve(newConfig); - } - }); + return this.get('fetchAddressOptions').perform(); + }, + fetchAddressOptions: task(function* () { + let store = this.get('store'); + try { + return yield store.find('option', 'address_options'); + } catch(err) { + if (err instanceof UnauthorizedError) { + throw err; + } + return store.push(this.get('defaultAddressOption')); + } + }).keepLatest().cancelOn('deactivate'), + defaultAddressOption: function() { + return this.get('store').normalize('option', { + id: 'address_options', + value: { + address1Label: this.get('i18n').t('admin.address.addressLabel'), + address1Include: true + } }); - } + }.property() });