Building Enterprise HTML5 Application

brite.registerDao

brite.registerDao(daoHandlerInstance)

Register a daoHandler Instance object with a specific entity type and return the corresponding DAO.

  • daoHandlerInstance a daoHandlerInstance for a single entity type. This object must have a .entityType() method or property that returned the entityType managed by this daoHandler instance.

returns {Dao} Returns the Dao object for this entityType.

Examples

var seedProjects = [
  {id:"001",title:"Grocery List"},
  {id:"002",title:"House Remodeling"},
  {id:"003",title:"Learn HTML5"},
  {id:"004",title:"Learn Brite"}
]

var seedTasks = [
  {id:"101",projectId:"001",done:false,title:"Heavy Whipping cream"},
  {id:"102",projectId:"001",done:true,title:"1 Garlic"},
  ...
  
  {id:"201",projectId:"002",done:true,title:"Take room dimensions"},
  ...    
]

// using the brite InMemoryDaoHandler (mostly for development/prototyping)
var projectDao = brite.registerDao(new brite.InMemoryDaoHandler("Project",seedProjects));
var taskDao    = brite.registerDao(new brite.InMemoryDaoHandler("Task",seedTasks));   

Once registered, the DAO object can be requested with brite.dao(ENTITY_TYPE)

var sameProjectDao = brite.dao("Project");
var sameTaskDao    = brite.dao("Task");  

DAOHandler implementation and best practice

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 model with the following rules.

  • Any method names containing with "delete, remove, update, create" will be considered datachange, and will consequently trigger the appropriate dataChange events on invocation.
  • Any methods returning a none Deferred promise will be wrapped as a Deferred Promise resolving with the result returned. This allow to normalize all CRUD apis to be asynchronous, even if the implementation did not need to.
  • DAOHandler can implement a isDataChange(methodName) to return true if the method name does not match the standard datachange naming convention but needs to be considered as one.

While a DAOHandler can implement any CRUD APIs, it is recommended that all DAOHandler implements at least the following as base. This allow better DAOHandler interchangibility.


/**
 * DAO Interface. Return value directly since it is in memory.
 * @param {String} objectType
 * @param {Integer} id
 * @return the entity
 */
MyDaoHandler.prototype.get = function(id){...

/**
 * DAO Interface: Create new object, set new id, and add it.
 *
 * @param {String} objectType
 * @param {Object} newEntity if null, does nothing
 */
MyDaoHandler.prototype.create = function(newEntity) {..

/**
 * DAO Interface: remove an instance of objectType for a given type and id.
 *
 * Return the id deleted
 *
 * @param {String} objectType
 * @param {Integer} id
 *
 */
MyMemoryDaoHandler.prototype.remove = function(id) {...

/**
 * Additional methods to remove multiple items
 * 
 * @param {Array} ids. Array of entities id that needs to be removed 
 * 
 * @return the array of ids that have been removed
 */
MyMemoryDaoHandler.prototype.removeMany = function(ids){..

/**
 * DAO Interface: update a existing id with a set of property/value data.
 *
 * The DAO resolve with the updated data.
 *
 * @param {String} objectType
 * @param {Object} data Object containing the id and the properties to be updated
 *
 * Return the new object data
 */
MyDaoHandler.prototype.update = function(data){...


/**
 * DAO Interface: Return a deferred object for this objectType and options
 * @param {String} objectType
 * @param {Object} opts
 *           opts.pageIndex {Number} Index of the page, starting at 0.
 *           opts.pageSize  {Number} Size of the page
 *           opts.match     {Object} Object of matching items. If item is a single value, then, it is a ===, otherwise, it does an operation
 *                                        {prop:"name",op:"contains",val:"nana"} (will match an result like {name:"banana"})
 *           opts.orderBy   {String}
 *           opts.orderType {String} "asc" or "desc"
 */
MyDaoHandler.prototype.list = function(opts){...

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

For example, the provided brite.InMemoryDaoHandler.js is a good example of a DAOHandler implementation. All its CRUD apis are synchronous, but in most cases, DAOHandler will be asynchronous in nature, and will return jQuery Deferred Promise.

Ask, learn, share about brite.js

Go to brite.js G+ community