Skip to content

Browser sample

Aaron Hanusa edited this page May 14, 2016 · 30 revisions

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

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

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

// example.js

var PersonDataProxy = function() {

  this.data = [];

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

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

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

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

  this.delete = 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 service class, 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'],
  onValidate: function(done) {
    if (this.name === "Fred Jones") {
      this.__invalidate("Name cannot be Fred Jones");
    }
    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([ 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: "Fred Jones"});

command.execute(function(err, result) {
  if (result.success) {
    console.log(result.value);
  } else {
    console.log(result.errors); // prints the errors as a result of the failed PersonNameRule
  }
});

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'],
  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([
        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: "Fred Jones", city: "Nowhere"});

command.execute(function(err, result) {
  if (result.success) {
    console.log(result.value);
  } else {
    console.log(result.errors); // prints the errors as a result of the failed PersonNameRule and ValidCityRule rules
  }
});

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: "Freida Jones", 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