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
userDao.update({id:123,firstName:"Mike"});

All DAO APIs Are Async Based

Brite turns all the DAOs API to be jQuery.Deferred

// update the user 123 with new firstName
userDao.update({id:123,firstName:"Mike"}).done(function(result){
  // on done (jQuery.Deferred api), we log a 
  console.log("user: " + result.id + " has a new firstName: " + result.firstName);
  
  userDao.list({match:{firstName:"Mike"}}).done(function(allMikes){
    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"
brite.dao.onDataChange("User,Task","create,update",function(event){
  var daoEvent = event.daoEvent;
  console.log("The action: " + daoEvent.action + 
              " was called on entity: " + daoEvent.entityType);
  console.log("the result is : " + daoEvent.result); 
},"a-event-namespace");
// Note: often, the event-namespace is the view.id

Unbinding is as simple as:

// unbinding a data change for a event namespace
brite.dao.offDataChange("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)
brite.dao.onDataChange("User,Task",function(event)... 

// Will bind datachange action, any entity
brite.dao.onDataChange(function(event)....

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