This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce ember-concurrency with examples (#977)
* Add ember-concurrency addon version 0.7.19 * 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. * Refactor patients/delete/controller to use e-c Example of using ember-concurrency tasks to manage a complex promise chain of events. I picked this one mainly because I wanted to illustrate the simplicity of using e-c tasks to manage what was otherwise a very complex promise chain. I tried to preserve some of the concurrency described in the previous promise based code. However, after some analysis and discussion on the Ember Slack channels difference between preserving the concurrency and just running the resolutions serially are likely very small. In this case the use of `all()` could likely be removed without a significant impact on performance. I mention this later optimisation as a way to make the code even easier to grok. I'll leave the debate to further discussion.
- Loading branch information
Showing
3 changed files
with
123 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,39 @@ | ||
import AbstractEditRoute from 'hospitalrun/routes/abstract-edit-route'; | ||
import Ember from 'ember'; | ||
import AbstractEditRoute from 'hospitalrun/routes/abstract-edit-route'; | ||
import { task } from 'ember-concurrency'; | ||
import { translationMacro as t } from 'ember-i18n'; | ||
import UnauthorizedError from 'hospitalrun/utils/unauthorized-error'; | ||
|
||
const { computed } = Ember; | ||
|
||
export default AbstractEditRoute.extend({ | ||
hideNewButton: true, | ||
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: computed(function() { | ||
return this.get('store').normalize('option', { | ||
id: 'address_options', | ||
value: { | ||
address1Label: this.get('i18n').t('admin.address.addressLabel'), | ||
address1Include: true | ||
} | ||
}); | ||
} | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters