Lego -- A node web modules framework
In web development, a web page is usually separated to lots of modules , head , article , footer , side ads...
And each module has individual data from db or service.
We fetch all these data and then render the template .
With Lego we made this easier.
Install
npm install node-lego --save
Usage
In your request handler :
var User = ;var OrderList = ;var Profile = ;var Lego = ; app;
In module user.js
:
var Brick = Brick;moduleexports = Brick;
In module orderlist.js
var Brick = Brick;moduleexports=Brick;
In profile.js
, do something same as orderlist;
In index.jade
div #Username orderlist !OrderListView profile !PrifileView
API
Lego.setting
global config with a get
and set
function. Config list:
view engine
, default 'jade' , same asexpress
, any template engin supported by consolidate is supported.views
, default 'views' , root path of the template files , same asexpress
debug
, default 'process.NODE_EVN == "development" ' , debug mode , if true , view render error stack will set to the view property of the final data.
use Lego.setting.get
and Lego.setting.set
to get and set this config.
example:
var Lego = ;var path = ;Legosetting;Legosetting;
Lego.Brick
Brick.create(name , handler);
name
{String} name of the module . An attribute with this name will set to the final data .handler
{Function} handle function ,params
andfinishCallback
will be parsed in. Call this callback function withstatus
anddata
.viewPath
{String} optional, template file path, joined tosetting.get("views")
. if this argument provided , the template will be auto rendered whenfinish(Brick.SUCCESS ,data)
called and a property Brick.Name+'View' will set to the final data with value as rendered html String.
example: define a module
var myBrick = Brick;
in view.jade
.userinfo
a.name(href='/user/#{id}') #{name}
.email #{email}
if this brick finish with Brick.SUCCESS , this view will be rendered with the data. and the final data in .done
will get this properties.
User: userId:123 name:"mangix" email:"maqh1988@gmail.com"UserView:'<div class="userinfo"><a href="/user/123">mangix</a><div class="email">maqh1988@gmail.com</div></div>'
Brick.SUCCESS
in handler , call finish
callback with Brick.SUCCESS when data fetching is success;
Brick.FAIL
in handler , call finish
callback with Brick.FAIL when an error occurs , and the data will be set to null
,template will not be rendered.
Brick.TIMEOUT
when an timeout
option passed to Lego , if Brick handle timeout , finish
will be called with TIMEOUT automatic. data will be null
.
Lego
constructor Lego(options)
options
{Object}
timeout:0 //brick timeout time ,default 0 , not control
lego.start(params)
params
{Object} initial data
start the module system with some initial data. Each Brick will get this data in handler.
start userId:1;
lego.pipe(brick1,brick2....)
brick
Brick
created by Brick.create()
execute each brick in the same time , and merge the data together.
pipe
could be called many times.
pipes are serial and in each pipe bricks are in parallel.
data will be merged and passed to the next.
example:
start id:1 //preBrick1 and preBrick2 will be executed at the same time //nextBrick1 and nextBrick2 will be executed at the same time when preBricks are all finished//and data finished by preBrick1 and preBrick2 will passed to nextBrick1 and nextBrick2
lego.top(brick,property,toProperty)
when Brick finish with Brick.SUCCESS and data , data will merge as follow:
Brick1Name:data Brick2Name:data
sometimes it's not convenient for the next Brick or template. You can use this method to convert the inside property to the top level.
brick
Brick create by Brick.create()property
src propertytoProperty
target property
example:
//in your user brick handle , you finish; //you have to use this data by data.User.userInfo.id , in the OrderBrick you have to :Brick; //with this api , after pipe UserBrick , call the `top` api to transfer userId to the top level.topUserBrick'userInfo.id''userId'; //now in OrderBrick var userId = paramsuserId; //you can also use OrderBrick in the first pipe,//because it's not depend on params.User.userInfo.idstart userId:123;
lego.done(callback)
callback
, callback function with data
when all pipes are finish , this callback will be called with the merged data
start id:1;