Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
- needs-async and needs-async-state contextual provider component
- find-all helper
- find-record helper
- belongs-to helper
- has-many helper
  • Loading branch information
dknutsen committed Mar 21, 2019
1 parent 72d48a2 commit 9fe0367
Show file tree
Hide file tree
Showing 36 changed files with 420 additions and 12,643 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module.exports = {
globals: {
server: true,
},
root: true,
parserOptions: {
ecmaVersion: 2017,
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Installation

* `git clone <repository-url>`
* `cd ember-async-providers`
* `cd ember-needs-async`
* `npm install`

## Linting
Expand All @@ -23,4 +23,4 @@
* `ember serve`
* Visit the dummy application at [http://localhost:4200](http://localhost:4200).

For more information on using ember-cli, visit [https://ember-cli.com/](https://ember-cli.com/).
For more information on using ember-cli, visit [https://ember-cli.com/](https://ember-cli.com/).
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ember-async-providers
ember-needs-async
==============================================================================

[Short description of the addon.]
Expand All @@ -15,7 +15,7 @@ Installation
------------------------------------------------------------------------------

```
ember install ember-async-providers
ember install ember-needs-async
```


Expand Down
7 changes: 7 additions & 0 deletions addon/components/needs-async-state.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Component from '@ember/component';
import layout from '../templates/components/needs-async-state';

export default Component.extend({
layout,
tagName: ''
});
9 changes: 9 additions & 0 deletions addon/components/needs-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Component from '@ember/component';
import layout from '../templates/components/needs-async';

export default Component.extend({
layout,
tagName: '',

needs: null,
});
15 changes: 15 additions & 0 deletions addon/helpers/belongs-to.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Helper from '@ember/component/helper';
import { task } from 'ember-concurrency';

export default Helper.extend({
belongsToTask: task(function * (model, relationship) {
return yield model.belongsTo(relationship).load();
}),
compute([model, relationship/*, ...rest*/]/*, hash*/) {
// TODO: better error handling
if(!model || !model.belongsTo || !relationship) {
return null;
}
return this.belongsToTask.perform(model, relationship);
}
});
13 changes: 13 additions & 0 deletions addon/helpers/find-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Helper from '@ember/component/helper';
import { inject as service } from '@ember/service';
import { task } from 'ember-concurrency';

export default Helper.extend({
store: service(),
findAllTask: task(function * (modelType) {
return yield this.store.findAll(modelType);
}),
compute([modelType/*, ...rest*/]/*, hash*/) {
return this.findAllTask.perform(modelType);
}
});
14 changes: 14 additions & 0 deletions addon/helpers/find-record.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Helper from '@ember/component/helper';
import { inject as service } from '@ember/service';
import { task } from 'ember-concurrency';

export default Helper.extend({
store: service(),
findRecordTask: task(function * (modelType, id) {
return yield this.store.findRecord(modelType, id);
}),
compute([modelType, id/*, ...rest*/]/*, hash*/) {
if(!id) return null;
return this.findRecordTask.perform(modelType, id);
}
});
15 changes: 15 additions & 0 deletions addon/helpers/has-many.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Helper from '@ember/component/helper';
import { task } from 'ember-concurrency';

export default Helper.extend({
hasManyTask: task(function * (model, relationship) {
return yield model.hasMany(relationship).load();
}),
compute([model, relationship/*, ...rest*/]/*, hash*/) {
// TODO: better error handling
if(!model || !model.hasMany || !relationship) {
return null;
}
return this.hasManyTask.perform(model, relationship);
}
});
3 changes: 3 additions & 0 deletions addon/templates/components/needs-async-state.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#if (get this.taskInstance state)}}
{{yield (get this.taskInstance state)}}
{{/if}}
6 changes: 6 additions & 0 deletions addon/templates/components/needs-async.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{yield (hash
taskInstance=this.needs
loading=(component "needs-async-state" state="isRunning" taskInstance=this.needs)
loaded=(component "needs-async-state" state="value" taskInstance=this.needs)
error=(component "needs-async-state" state="error" taskInstance=this.needs)
)}}
1 change: 1 addition & 0 deletions app/components/needs-async-state.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'ember-needs-async/components/needs-async-state';
1 change: 1 addition & 0 deletions app/components/needs-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'ember-needs-async/components/needs-async';
1 change: 1 addition & 0 deletions app/helpers/belongs-to.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default, belongsTo } from 'ember-needs-async/helpers/belongs-to';
1 change: 1 addition & 0 deletions app/helpers/find-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default, findAll } from 'ember-needs-async/helpers/find-all';
1 change: 1 addition & 0 deletions app/helpers/find-record.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default, findRecord } from 'ember-needs-async/helpers/find-record';
1 change: 1 addition & 0 deletions app/helpers/has-many.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default, hasMany } from 'ember-needs-async/helpers/has-many';
Loading

0 comments on commit 9fe0367

Please sign in to comment.