-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ilya Radchenko
committed
Feb 25, 2017
1 parent
cd3d162
commit 43fbee4
Showing
5 changed files
with
147 additions
and
23 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Contributing | ||
|
||
This README outlines the details of collaborating on this Ember addon. | ||
|
||
## Installation | ||
|
||
* `git clone <repository-url>` this repository | ||
* `cd ember-data-tasks` | ||
* `npm install` | ||
* `bower install` | ||
|
||
## Running | ||
|
||
* `ember serve` | ||
* Visit your app at [http://localhost:4200](http://localhost:4200). | ||
|
||
## Running Tests | ||
|
||
* `npm test` (Runs `ember try:each` to test your addon against multiple Ember versions) | ||
* `ember test` | ||
* `ember test --server` | ||
|
||
## Building | ||
|
||
* `ember build` | ||
|
||
For more information on using ember-cli, visit [https://ember-cli.com/](https://ember-cli.com/). |
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,27 +1,78 @@ | ||
# ember-data-tasks | ||
|
||
This README outlines the details of collaborating on this Ember addon. | ||
An alternative Ember Data store that returns [Ember Concurrency] tasks instead of promises. | ||
|
||
## Installation | ||
## Install | ||
|
||
* `git clone <repository-url>` this repository | ||
* `cd ember-data-tasks` | ||
* `npm install` | ||
* `bower install` | ||
```sh | ||
ember install ember-data-tasks | ||
``` | ||
|
||
## Running | ||
## Usage | ||
|
||
* `ember serve` | ||
* Visit your app at [http://localhost:4200](http://localhost:4200). | ||
Override your existing store, by creating a new store: | ||
|
||
## Running Tests | ||
```sh | ||
ember g service store | ||
``` | ||
|
||
* `npm test` (Runs `ember try:each` to test your addon against multiple Ember versions) | ||
* `ember test` | ||
* `ember test --server` | ||
Open `app/services/store.js` and modify it to: | ||
|
||
## Building | ||
```js | ||
import Store from 'ember-data-tasks/services/store'; | ||
|
||
* `ember build` | ||
export default Store; | ||
``` | ||
|
||
For more information on using ember-cli, visit [https://ember-cli.com/](https://ember-cli.com/). | ||
Now you can use your Ember Data like before, and nothing has changed. | ||
But if you want immediate responses, you will have to wrap your store | ||
responses in a hash. | ||
|
||
The example below will hit `afterModel` after the backend has resolved with data: | ||
|
||
```js | ||
import Ember from 'ember'; | ||
|
||
export default Ember.Route.extend({ | ||
model() { | ||
return this.store.findAll('post'); | ||
} | ||
}); | ||
``` | ||
|
||
But if you want immediate responses, do the following, and `afterModel` will | ||
be hit immediately: | ||
|
||
```js | ||
import Ember from 'ember'; | ||
|
||
export default Ember.Route.extend({ | ||
model() { | ||
return { | ||
postsTask: this.store.findAll('post') | ||
}; | ||
} | ||
}); | ||
``` | ||
|
||
Then you can utilize the `task` in your template like so: | ||
|
||
```hbs | ||
<ul> | ||
{{#if model.postsTask.isRunning}} | ||
Loading.. | ||
{{else}} | ||
{{#each model.postTask.value as |post|}} | ||
<li>{{post.name}}</li> | ||
{{/each}} | ||
{{/if}} | ||
</ul> | ||
``` | ||
|
||
This seems like a slight annoyance at first, due to the extra level of nesting, but | ||
in the end if you build ambitious apps, you will most likely return multiple | ||
tasks, and would have used `Ember.RSVP.hash` if working with promises. | ||
|
||
**Note: You can unwrap the task hash in `setupController`, if it really bothers you.** | ||
|
||
[Ember Concurrency]: http://ember-concurrency.com |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import DS from 'ember-data'; | ||
import { task } from 'ember-concurrency'; | ||
|
||
export default DS.Store.extend({ | ||
findAll() { | ||
let promise = this._super(...arguments); | ||
return this.get('wrap').perform(promise); | ||
}, | ||
|
||
query() { | ||
let promise = this._super(...arguments); | ||
return this.get('wrap').perform(promise); | ||
}, | ||
|
||
findRecord() { | ||
let promise = this._super(...arguments); | ||
return this.get('wrap').perform(promise); | ||
}, | ||
|
||
queryRecord() { | ||
let promise = this._super(...arguments); | ||
return this.get('wrap').perform(promise); | ||
}, | ||
|
||
wrap: task(function * (promise) { | ||
let result = yield promise; | ||
return result; | ||
}) | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { moduleFor, test } from 'ember-qunit'; | ||
|
||
moduleFor('service:store', 'Unit | Service | store', { | ||
// Specify the other units that are required for this test. | ||
// needs: ['service:foo'] | ||
}); | ||
|
||
// Replace this with your real tests. | ||
test('it exists', function(assert) { | ||
let service = this.subject(); | ||
assert.ok(service); | ||
}); |