Skip to content

New in 2.0

ahanusa edited this page Feb 8, 2019 · 9 revisions

Below are the new features and improvements in peasy-js 2.0.

Command.getErrors()

The getErrors function has been introduced to support cases that require immediate feedback, especially useful for input validation notifications.

Below is an example of sample usage:

var command = service.insertCommand({ requiredField: null});

command.getErrors().then(errors => {
  console.log("ERRORS", errors);
});

TypeScript support

Typescript definitions are now supplied with peasy-js 2.0 by default. You can checkout the definition file and samples here:

Promise support

peasy-js 2.0 now supports promises in addition to its legacy continuation callback pattern.

For more information and examples of how to use promises with peasy-js, see:

AutoPromiseWrap

Many times when you write functions in your rules, commands, and business services, you may not have asynchronous operations to perform. Let's take a look at an example:

class PersonNameRule extends Rule {

  constructor(name) {
    super();
    this.name = name;
  }

  _onValidate() {
    if (this.name === "Jimi Hendrix") {
      this._invalidate("Name cannot be Jimi Hendrix");
    }
    return Promise.resolve();
  }
}

class CustomerService extends BusinessService {
  constructor(dataProxy) {
    super(dataProxy);
  }

  _getRulesForInsertCommand(customer, context) {
    return Promise.resolve([
      new PersonNameRule(customer.name);
    ]);
  }

  _getRulesForUpdateCommand(customer, context) {
    return Promise.resolve([
      new PersonNameRule(customer.name);
    ]);
  }
}

Notice how our function implementations require us to return promises even though we aren't performing any asynchronous operations. In these instances, we can have promises automatically returned on our behalf by using the autoPromiseWrap functionality.

To enable auto promise wrapping:

  • Import or reference peasy-js.Configuration from peasy.js.
  • Set Configuration.autoPromiseWrap = true. (this only needs to be done once per application lifetime)
  • Write your function definition without explicitly returning a promise

In your application startup:

/// import or reference peasy-js Configuration

Configuration.autoPromiseWrap = true;

Function implementations with autoPromiseWrap enabled can now become:

class PersonNameRule extends Rule {

  constructor(name) {
    super();
    this.name = name;
  }

  _onValidate() {
    if (this.name === "Jimi Hendrix") {
      this._invalidate("Name cannot be Jimi Hendrix");
    }
  }
}

class CustomerService extends BusinessService {
  constructor(dataProxy) {
    super(dataProxy);
  }

  _getRulesForInsertCommand(customer, context) {
    return [
      new PersonNameRule(customer.name)
    ];
  }

  _getRulesForUpdateCommand(customer, context) {
    return [
      new PersonNameRule(customer.name)
    ];
  }
}

Signature changes

Changes have been made to many of the function definitions of Rule, Command, and BusinessService to provide more clarity and better parity between peasy-js and other peasy frameworks. See 2.0 migration for more details.