• Blog
  • Docs
  • Updates
Webda
Docs Menu
  • Quick start
    • Tutorials
  • Develop
    • Authentication
    • Binaries
    • Custom
    • Mailer
    • Models
    • Polymer
    • Queues
    • Stores
  • Deploy
    • AWS
    • Docker
    • Configuration Resolution
    • Local
    • WeDeploy

Models Guide

Implement your business logic directly in your Model

Overview

Model is the best way to express your business logic.

Stores will use them to load/save/validate your objects and access to it. If no model are specified to a Store it will use the default CoreModel

Security

The model has a predefined method canAct that will be called whenever an action is trigger on an object from an external source

This method return a Promise that will stop the processing if it is rejected

class CoreModel {
  canAct(ctx, action) {
    if (action === 'create') {
      return this.canCreate(ctx);
    } else if (action === 'update') {
      return this.canUpdate(ctx);
    } else if (action === 'get') {
      return this.canGet(ctx);
    } else if (action === 'delete') {
      return this.canDelete(ctx);
    }
  }
}

Custom Actions

The model can defined action that will be exposed by its Store

class CoreModel {
    static getActions() {
      return {
        'push': {method: 'POST'},
        'qrcode': {method: ['GET', 'PUT']}
      };
    }
}

Store Events

The model can defined behavior on store event without defining a listener The _onAction and _onActioned are not defined as the action by itself is already inside the object

class CoreModel {
    _onSave() {
      // Will be called beforeSave
    }
    _onSave() {
      // Will be called afterSave
    }
    _onUpdate() {
      // Will be called beforeUpdate
    }
    _onUpdated() {
      // Will be called afterUpdate
    }
    _onDelete() {
      // Will be called afterDelete
    }
    _onDeleted() {
      // Will be called afterDelete
    }
    _onGet() {
      // Will be called when an object is retrieved
    }
}

Contribute on Github! Edit this section.