Created
October 14, 2021 13:36
-
-
Save yarigpopov/644e6c7dc6150880768481c0f61322b3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import Service from '@ember/service'; | |
import { tracked } from '@glimmer/tracking'; | |
export default class DogDataService extends Service { | |
@tracked data: IDog = null; | |
@tracked status: Status = Status.loading; | |
@tracked error: Error = null; | |
constructor() { | |
super(...arguments); | |
this.loadDogs(); | |
} | |
get isLoading() { | |
return this.status === Status.loading; | |
} | |
get didError() { | |
return this.status === Status.error; | |
} | |
get isLoaded() { | |
return this.status === Status.loaded; | |
} | |
// usually we would use ember-concurrency for this | |
// http://ember-concurrency.com/docs/introduction | |
private async loadDogs() { | |
try { | |
const asyncMockApiFn = async (): Promise<IDog> => | |
await new Promise(resolve => setTimeout(() => resolve(DATA), 1000)); | |
const data = await asyncMockApiFn(); | |
this.data = data; | |
this.error = null; | |
this.status = Status.loaded; | |
} catch (error) { | |
this.data = null; | |
this.error = error; | |
this.status = Status.error | |
} | |
} | |
} |
This file contains hidden or 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
import Component from '@glimmer/component'; | |
import { inject as service } from '@ember/service'; | |
import DogDataService from '../services/dog-data'; | |
export default class Profile extends Component { | |
@service declare readonly dogData: DogDataService; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment