bmodulejs
v2.1.1
Published
Browser module Controller, which surpport a good way for code tracking
Downloads
10
Maintainers
Readme
BModuleJs 2.1.0
About
A Controller For Modules.You can use it as javascript module controller, which can help you to make your code more structured. You can also use it to tracking code by use the function "module.tailLog('process')" so that you can see the running log of the modules.
# You can get it by:
npm install bmodulejs
Optimization of 2.1.0
- Simplify the way to define a module.
- Simplify the way to use other module function by $M.mods.
- Change the way to deal with those variable returned by "create".
API
$M.defineModule( mNameStr );
Define a new module object.The same with old "$M.define".
@param {string} [mNameStr] module name, use "/" to split namespace.
@return {object}
var module = $M.defineModule( 'page/home/main' );
$M.define( mNameStr )
Define a new module and set it.
@param {string} [mNameStr] module name, use "/" to split namespace.
@return {object}
$M.define( 'page/home/main', function (module, $T, mods, ms) {
// Dependence treatment
module.require('common/database');
return function () {
// use the module's function by "mods", the same with $M.mods
var database = new mods.common.database( dataConf );
// show module's log by "ms", the same with $M.modules.
ms.common.database.showLog();
return {
state: 'finished'
};
};
}).create();
If the returned value is a function or depends on other module,you must use a function to wrap it. In other cases, you can just return it.
$M.newM
Get a module object.
$M.create( nameStr, spec, cbk );
Create a module which you have defined by function "$M.define"
@param {string} [nameStr] module name.
@param {array|all} [spec] parameter for modules on creating.
@param {function} [cbk] callback
$M.modules;
A namespace for modules.All of the modules will be added to this object.
So, you can use those modules you added by this way.
$M.mods;
Namespace for the return value by module's "create" function.
$M.destroy( nameStr );
The way to destroy a module.
$M.expendT( fnName, fn );
The way to expend $M.tools.
$M.getModule( nameStr );
The way to get module object by name string.
$M.getMod( nameStr )
The way to get a defined module's function or other data.
$M.jsLoader( src, conf );
Js loader.
@param {object} conf
{
'cbk' : function (){},
'isAsync' : false,
'isClear' : false,
'sudo' : false'
}
$M.options();
Settings.
$M.path();
Javascript src setting.
$M.reload( nameStr );
Reloading those modules which loaded by bmodulejs.
$M.tools;
Some tools functions.
Dependence treatment
eg.
$M.define('home/init', function (module, $T, mods, ms) {
// get it when "create" be called.
module.require( 'common/database' );
return function () {
console.log( arguments );
var nd = document.getElementById( 'example' );
var dataConf = {
// index keys
'idx' : ['num', 'name', 'age']
};
// create database
var database = new mods.common.database( dataConf );
for( var i = 0; i < 99999; i++ ) {
database.create( {
'num' : i,
'name' : 'BottleLiu' + i,
'age' : i % 24
} );
}
var str = '';
var strTotal = 'Data length: ' + database.total + '<br><br>';
var strGet = 'The length of data which age equal 23: ' + database.get({'age' : 23}).length;
str = strTotal + strGet;
nd.innerHTML = str;
return database;
};
}).create('Arguments for init function.');
Strapping Tools
Step 1.the first time you use this tools
# enter dir
cd xxxx
npm install
Step 2.
grunt bmodule:filepath[:target:prefix:isUglify]
# filepath: the file or path you want to strap.
# target: the target path or file path for strapped file.
# prefix: the same with "$M.options('sourceRoot', 'static/js/');" value.
# isUglify: need to compress.use "0" or "1" to turn off or turn on it.
eg.
grunt bmodule:static/js/::static/js/:0
Skills
Script tag
<!--Use bmodule to load js.the attribute "after" will be loaded after "msrc"-->
<script src="source/bmodulejs-2.1.0.js" msrc="static/js/common/base.js" after="static/js/home/init.js"></script>