An open API service indexing awesome lists of open source software.

https://github.com/julienblin/ng-async-event


https://github.com/julienblin/ng-async-event

angular angular2 angular4 angular5 npm rxjs rxjs-observables

Last synced: 3 months ago
JSON representation

Awesome Lists containing this project

README

        

# ng-async-event

Angular UI component that simplifies usage of [rx-async-event](https://github.com/julienblin/rx-async-event).

[![Travis](https://travis-ci.org/julienblin/ng-async-event.svg?branch=master)](https://travis-ci.org/julienblin/ng-async-event)
[![npm](https://img.shields.io/npm/v/ng-async-event.svg)](https://www.npmjs.com/package/ng-async-event)

## Objectives

This library is a complement to [rx-async-event](https://github.com/julienblin/rx-async-event) that allows the projection of
the state management in [rx-async-event](https://github.com/julienblin/rx-async-event) into dedicated, re-usable templates.

## How to use

### Installation

```shell
npm i --save ng-async-event
```

### Import the NgAsyncEventModule in the main app module

```typescript
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
NgAsyncEventModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
```

### Service

```typescript
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { AsyncEventObservable, AsyncEventSubject } from 'rx-async-event';
import { Post } from './post';

@Injectable()
export class AppService {

private readonly _postEvent$ = new AsyncEventSubject();

constructor(private http: HttpClient) { }

get postEvent$(): AsyncEventObservable {
return this._postEvent$.asObservable();
}

/**
* Example of managing the life cycle of an HttpClient Observable.
*/
loadPost(id: number) {
return this._postEvent$.observe(
id,
this.http.get(`https://jsonplaceholder.typicode.com/posts/${id}`));
}

/**
* This example manages the life cycle of a Promise instead.
*/
loadPostAsPromise(id: number) {
return this._postEvent$.execute(
id,
(arg) => this.http.get(`https://jsonplaceholder.typicode.com/posts/${id}`).toPromise());
}

}
```

### Component

```typescript
import { Component } from '@angular/core';
import { AppService } from './app.service';
import { Post } from './post';
import { AsyncEventObservable } from 'rx-async-event';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [ AppService ]
})
export class AppComponent {

postEvent$: AsyncEventObservable;

constructor(private appService: AppService) {
this.postEvent$ = this.appService.postEvent$;
}

loadPost(id: number) {
this.appService.loadPost(id);
}
}
```

### View

```html


Processing post id {{ postId }}...


Post title: {{ post.title }}

```

## Default templates

It is also possible to define default templates that will be used if no local template is defined.

In the `app.component.html` file, define them using ``:

```html




{{ result }}


{{ error }}

```

and then later:

```html

```

It is also possible to create alternate defaults set using the `setName` input:

```html


Loading {{ argument }}...


{{ result | json }}


{{ error }}

{{ error.stack }}

```

and then later reference the `setName` in ``:

```html

```

## Template context

The context is passed to templates using [Angular Microsyntax](https://angular.io/guide/structural-directives#microsyntax).

Please see [async-event-template-context.ts](src/app/modules/ng-async-event/async-event-template-context.ts) for the complete object.