Skip to content

Browser sample

Aaron Hanusa edited this page Jun 9, 2016 · 30 revisions

This complete example can be viewed on plnkr.

Start by adding a script reference to peasy.js or import it using your script loader of choice.

Alternatively, you can install peasy-js via bower: bower install peasy-js

Next, create an example.js file and set a script reference to or import it. Alternatively, create an individual file for each module, and set script references to/import them accordingly.

Within example.js, create a data proxy with the following content:

// personDataProxy.js

var PersonDataProxy = function() {

  this.data = [];

  this.getById = function(id, done) {
    var person = this.findBy(id);
    done(null, person);
  };

  this.getAll = function(done) {
    done(null, this.data);
  };

  this.insert = function(data, done) {
    data.id = this.data.length + 1;
    this.data.push(data);
    done(null, data);
  };

  this.update = function(data, done) {
    var person = this.findBy(data.id);
    person.name = data.name;
    done(null, person);
  };

  this.destroy = function(id, done) {
    var person = this.findBy(id);
    var index = this.data.indexOf(person);
    this.data.splice(index, 1);
    done();
  };

  this.findBy = function(id) {
    var person = this.data.filter((function(p) {
      return p.id === id;
    }))[0];
    return person;
  };

};

Next create a business service, which exposes CRUD commands responsible for subjecting data proxy invocations to business rules before execution:

// personService.js

var PersonService = peasy.BusinessService.extend().service;

Now let's consume our PersonService by adding the following contents:

// example.js

var personService = new PersonService(new PersonDataProxy());
var command = personService.insertCommand({name: "James Morrison"});

command.execute(function(err, result) {
  if (result.success) {
    console.log(result.value); // prints the inserted object with the assigned id
  }
});

Take a moment and run the application to view the output in your browser console.

Let's create a business rule whose execution must be successful before the call to inject dataproxy.insert() function is invoked.

// personNameRule.js
var PersonNameRule = peasy.Rule.extend({
  association: "name",
  params: ['name'],
  functions: {
    _onValidate: function(done) {
      if (this.name === "Jimi Hendrix") {
        this._invalidate("Name cannot be Jimi Hendrix");
      }
      done();
    }
  }
});

And wire it up in our PersonService to ensure that it gets fired before inserts:

// personService.js

var PersonService = peasy.BusinessService.extend({
  functions: {
    _getRulesForInsert: function(person, context, done) {
      done(null, new PersonNameRule(person.name));
    }
  }
}).service;

And update our testing code

// example.js

var dataProxy = new PersonDataProxy();
var personService = new PersonService(dataProxy);
var command = personService.insertCommand({name: "Jimi Hendrix"});

command.execute(function(err, result) {
  if (result.success) {
    console.log(result.value);
  } else {
    console.log(result.errors[0]); // prints {association: "name", message: "Name cannot be Jimi Hendrix"}
  }
});

Take a moment and run the application to view the output in your browser console.

Let's create one more rule, just for fun:

// validCityRule.js

var ValidCityRule = peasy.Rule.extend({
  association: "city",
  params: ['city'],
  functions: {
    _onValidate: function(done) {
      if (this.city === "Nowhere") {
        this._invalidate("Nowhere is not a city");
      }
      done();
    }
  }
});

We'll associate this one with inserts too:

// personService.js

var PersonService = peasy.BusinessService.extend({
  functions: {
    _getRulesForInsert: function(person, context, done) {
      done(null, [
        new PersonNameRule(person.name),
        new ValidCityRule(person.city)
      ]);
    }
  }
}).service;

And update our testing code

// example.js

var dataProxy = new PersonDataProxy();
var personService = new PersonService(dataProxy);
var command = personService.insertCommand({name: "Jimi Hendrix", city: "Nowhere"});

command.execute(function(err, result) {
  if (result.success) {
    console.log(result.value);
  } else {
    console.log(result.errors[0]); // prints {association: "name", message: "Name cannot be Jimi Hendrix"}
    console.log(result.errors[1]); // prints {association: "city", message: "Nowhere is not a city"}
  }
});

Take a moment and run the application to view the output in your browser console.

Finally, let's pass in valid data and watch it be a success

// example.js
var dataProxy = new PersonDataProxy();
var personService = new PersonService(dataProxy);
var command = personService.insertCommand({name: "James Hendrix", city: "Madison"});

command.execute(function(err, result) {
  if (result.success) {
    console.log(result.value); // prints the inserted object with the assigned id
  } else {
    console.log(result.errors);
  }
});

Take a moment and run the application to view the output in your browser console.

Clone this wiki locally