TinyCore.js - Register/Start Modules (intermediate)
HTML
<script src="https://raw.github.com/mawrkus/tinycore/master/src/TinyCore.js"></script>
<div id="container">
<h3>Press buttons to see the messages in the "console"...</h3>
<ol>
<li><button id='startModuleA'>Start Module 'module-A'</button> → press button to start the module</li>
<li><button id='startModuleB'>Start Module 'module-B'</button> → press button to start the module</li>
</ol>
</div>
<a href="#" id="clear_logs">[clear logs]</a>
<div id="logs"></div>
CSS
#container { padding:10px; font-family: Arial; }
#logs { height:500px; margin:20px 10px; padding:10px; font-family: Courier; font-size:11px; border:1px solid #000; overflow-y:scroll; }
em { font-family: Courier; font-size:13px; font-weight:bold; }
h3 { font-style: italic; margin-bottom:10px; }
#clear_logs { position: absolute; right: 9px; margin-top: -5px; }
JavaScript
var oLogs = document.getElementById( 'logs' );
/* --- REGISTER MODULES --------------------------------- */
// Registering a VALID Module
TinyCore.Module.register( 'module-A', function( bus ){
return {
onStart: function( oData ) {
// Code that will be executed when this module is started.
logIt ( new Date(), "blue" );
logIt ("Module 'module-A' Started", "green");
oData && oData.msg ? logIt ("msg: " + oData.msg) : null;
logIt ("<br/>");
}
};
});
logIt ( new Date(), "blue" );
logIt ("Module 'module-A' Registered", "brown");
logIt ("...this module is now ready to be started", "grey");
logIt ("<br/>");
// Registering a VALID Module
TinyCore.Module.register( 'module-B', function( bus ){
return {
onStart: function( oData ) {
// Code that will be executed when this module is started.
logIt ( new Date(), "blue" );
logIt ("Module 'module-B' Started", "green");
oData && oData.msg ? logIt ("msg: " + oData.msg) : null;
logIt ("<br/>");
}
};
});
logIt ( new Date(), "blue" );
logIt ("Module 'module-B' Registered", "brown");
logIt ("...this module is now ready to be started", "grey");
logIt ("<br/>");
// Registering an INVALID Module
try {
TinyCore.Module.register( 'module-C', 'bla-bla-bla....' );
}
catch (eError) {
logIt ( new Date(), "blue" );
logIt (eError, "red");
logIt ("<br/>");
}
// Registering an INVALID Module
try {
TinyCore.Module.register( 'module-D', { prop1: 'aaa' } );
}
catch (eError) {
logIt ( new Date(), "blue" );
logIt (eError, "red");
logIt ("<br/>");
}
/* --- DOM READY --------------------------------- */
$(document).ready(function () {
$("#startModuleA").click(function () {
// Starting the Module
TinyCore.Module.start("module-A", { msg: "SOME message" });
return false;
});
$("#startModuleB").click(function () {
...