Building Enterprise HTML5 Application

Brite DAOs

Brite DAO Patterns

Brite provides a simple, extensible, and async way to access data through a DAO pattern.

One DAO Object Per Entity Type

An entityType can map to a business object, table, or object store (in indexDb).

// get the userDao (singleton pattern, so, can be reused anywhere)  
var userDao = brite.dao("User");

// call a dao the update

All DAO APIs Are Async Based

Brite turns all the DAOs API to be jQuery.Deferred

// update the user 123 with new firstName
  // on done (jQuery.Deferred api), we log a 
  console.log("user: " + + " has a new firstName: " + result.firstName);
    console.log("We have " + allMikes.length + " Mike");

Brite DAO Offers A Powerful Eventing System

All brite.dao.on*** accept the same (actions,entityTypes,callback,namespace) method signature

// binding on data change event 'create' and 'update' for entities "User" "Task"
  var daoEvent = event.daoEvent;
  console.log("The action: " + daoEvent.action + 
              " was called on entity: " + daoEvent.entityType);
  console.log("the result is : " + daoEvent.result); 
// Note: often, the event-namespace is the

Unbinding is as simple as:

// unbinding a data change for a event namespace

// Note: often, dao unbinding happens at the view.destroy

The actions or entityTypes are optional (if ommitted or null, "any" will be assumed)

// Will bind for any dataChange event (create, update, delete)

// Will bind datachange action, any entity

There is also brite.dao.onResult which is triggred on all dao calls regardless if they are datachange (so, create, get, list, or any other method)

And finally, the brite.dao.onDao, an advanced binding which will trigger on any dao call before the result is resolved.

A DAO is created by registering a DaoHandler for an entityType

A DaoHandler is per store type, and often an application will extends or implement their own DaoHandler for their local or remote API.

// register this only once at application intialization  
brite.registerDao(new brite.InMemoryDaoHandler("User"));

A DaoHandler implements CRUD API

A DAOHandler is a object that provides a set of CRUD methods usually for an given store or remote service. It needs to be instanciated for any given entityType to be registered with brite.

A DAOHandler must only implement the .entityType() method which must return the entityType managed by the instance. Any other methods will be considered "CRUD" method and wrapped by brite to enable asynchronous (i.e. Deferred) and eventing data programing model.

function MyDaoHandler(entityType){
  this._entityType = entityType;

// only mandatory method of a DAOHandler
MyDaoHandler.entityType = function(){
  return this._entityType;

// vvvvvvvv DAOHandler CRUD API Recommendation vvvvvvv

// Note: Any method name containing "remove, delete, update, or create" 
//       will be considered "dataChange" and brite will trigger the event appropriately

MyDaoHandler.prototype.get = function(id){
  // should return the entity for this id

MyDaoHandler.prototype.create = function(newEntity) {
  // should create the new entity, and probably return it. 

// ^^^^^^^ DAOHandler CRUD API Recommendation ^^^^^^

A good practice for DAOHandler is to be prototype based (as above) as this will allow them to be extended for more specific needs.

More info in the brite.registerDao documentation, and also see an implementation at brite.InMemoryDaoHandler.js

The Tutorial

Ask, learn, share about brite.js

Go to brite.js G+ community