Skip to content

Getting started

Mihael Šafarić edited this page Feb 20, 2019 · 2 revisions

The following examples describe the basic ngx-hal setup with the setup for CRUD operations on User resource.

Datastore service needs to be provided, usually it is provided in the application's top-most module.

import { NgModule } from '@angular/core';
import { DatastoreService } from 'ngx-hal';

@NgModule({
  declarations: [...],
  imports: [...],
  providers: [
    {
      provide: DatastoreService,
      useClass: HalDatastoreService, // your own class which extends DatastoreService
      deps: [
        HttpClient
      ]
    }
  ]
})
export class AppModule { }

Example of your DatastoreService class:

import { HttpClient } from '@angular/common/http';
import { DatastoreService, DatastoreConfig } from 'ngx-hal';

@DatastoreConfig({})
export class HalDatastoreService extends DatastoreService {
  constructor(public httpClient: HttpClient) {
    super(httpClient);
  }
}

Your models must extends HalModel. Model properties must be decorated with one of the following decorators: Attribute, HasOne, HasMany

import { HalModel } from 'ngx-hal';

export class User extends HalModel {
  @Attribute()
  public name: string;
}

You want to have a dedicated service which will handle all the external operations for the resource. Such service must extend ModelService,

import { Injectable } from '@angular/core';
import { ModelService, DatastoreService } from 'ngx-hal';
import { User } from '../../models/user.model';

@Injectable()
export class UserService extends ModelService<User> {
  constructor(datastore: DatastoreService) {
    super(datastore, User);
  }
}

TODO describe available operations on UserService