Skip to content

6. Extending Client Models

Jonathan Casarrubias edited this page May 17, 2017 · 3 revisions

LoopBack SDK Builder

Extending Models

One of the new great features of the LoopBack SDK Builder it that allows you to extend models in order to add new functionality within models.

In order to avoid working directly on models and lose information when re-generating the sdk, you will need to create a new extended folder within shared at the same level of sdk and create a model.extended.ts file:

import { Model } from '../../';

export class ModelExtended extends Model {

  constructor(json: Model) { super(json); }

  customFunction() {
    return `${this.name}: ${this.description}`;
  }
}

And then I implemented in the component as follows:

import { Model, ModelApi, ModelExtended , LoopBackConfig } from './shared';

...

  constructor(private modelApi: ModelApi) {
    LoopBackConfig.setBaseURL('http://localhost:3000');
    LoopBackConfig.setApiVersion('api');
    modelApi
      .create({ 
        name: 'My Model',
        description: 'This is my super description'
      })
      .map((model: Model) => new ModelExtended(model))
      .subscribe((modelExtended: ModelExtended) => {
        console.log(modelExtended.customFunction());
      });
  }
Clone this wiki locally