Introduced AppShall

This commit is contained in:
Borys Levytskyi
2015-04-04 16:10:17 +03:00
parent a3ca46174b
commit 17fc0bad97
13 changed files with 80 additions and 75 deletions

View File

@@ -1,54 +1,16 @@
(function (should, Container) {
(function (should, Container, AppShell) {
var app = {
views: {},
models: {},
debugMode: false
};
var di = new Container();
var app = new AppShell(di);
var appModules = [];
var runObservers = [];
app.di = new Container();
app.component = function(name, inst) {
if(arguments.length == 1) {
return this.di.resolve(name);
}
this.di.register(name, inst);
};
app.get = function(name) {
return this.di.resolve(name);
};
app.compose = function (module) {
appModules.push(module);
};
app.run = function(observer) {
runObservers.push(observer);
};
app.debugMode = false;
app.bootstrap = function(rootViewElement) {
this.rootViewElement = rootViewElement;
initializeModules();
invokeRunObservers();
this.initialize();
};
function initializeModules() {
appModules.forEach(function(m) {
if(is.aFunction(m)) {
m();
}
});
}
function invokeRunObservers() {
runObservers.forEach(function(o){ o(); });
}
window.app = app;
})(window.should, window.Container);
})(window.should, window.Container, window.AppShell);

View File

@@ -1,5 +1,5 @@
(function(app, should){
app.component('calc', {
app.set('calc', {
numberOfBits: function (num) {
should.bePositiveInteger(num);

View File

@@ -2,7 +2,7 @@
var twoOperandsRegex = /^(\d+)\s*(<<|>>|\||\&|\^)\s*(\d+)$/;
var numbersList = /^((\d*)+\s?)+$/;
app.component('expression', {
app.set('expression', {
canParse: function(string) {
return twoOperandsRegex.test(string) || numbersList.test(string);
},

View File

@@ -1,6 +1,8 @@
(function(should, app){
app.component("formatter", {
app.compose(function() {
app.set("formatter", {
toBinaryString: function(num, totalLength) {
var binaryStr = num.toString(2),
@@ -22,6 +24,8 @@
return formatted.join('');
}
});
})
})(window.should, window.app);

View File

@@ -1,7 +1,7 @@
app.compose(function() {
app.controller('expressionInputCtrl', function (){
var dispatcher = app.component('dispatcher');
var dispatcher = app.get('dispatcher');
return {
onViewAttached: function () {
@@ -54,7 +54,7 @@ app.compose(function() {
});
app.controller('resultViewCtrl', function() {
var html = app.component('html');
var html = app.get('html');
var resultViewController = {
$html: null,

View File

@@ -1,7 +1,7 @@
app.compose(function() {
app.component('dispatcher', function() {
var handlers = [];
var is = app.component('is');
var is = app.get('is');
var resultView = app.controller('resultViewCtrl');
var dispatcher = {

View File

@@ -1,7 +1,7 @@
(function(app, HtmlBuilder){
app.component('html', HtmlBuilder);
app.component('is', is);
app.set('html', HtmlBuilder);
app.set('is', is);
/*
var template = {
compile: function (template) {

View File

@@ -1,9 +1,9 @@
// Expression View
app.compose(function () {
var formatter = app.component('formatter');
var calc = app.component('calc');
var html = app.component('html');
var formatter = app.get('formatter');
var calc = app.get('calc');
var html = app.get('html');
app.modelView(app.models.BitwiseOperation, {
renderView: function(expr) {

36
core/appShell.js Normal file
View File

@@ -0,0 +1,36 @@
(function() {
function AppShell(diContainer) {
this.models = {};
this.di = diContainer;
this.runList = [];
this.compositionList = [];
}
AppShell.prototype.get = function(name) {
return this.di.resolve(name);
};
AppShell.prototype.set = function(name, def) {
this.di.register(name, def);
};
AppShell.prototype.run = function(func) {
this.runList.push(func);
};
AppShell.prototype.compose = function (func) {
this.compositionList.push(func);
};
AppShell.prototype.initialize = function () {
callInvocationList(this.compositionList);
callInvocationList(this.runList);
};
function callInvocationList(functions) {
functions.forEach(function(o){ o(); });
}
window.AppShell = AppShell;
})();

View File

@@ -5,10 +5,11 @@
<title></title>
<script type="text/javascript" src="components/is.js"></script>
<script type="text/javascript" src="components/should.js"></script>
<script type="text/javascript" src="core/is.js"></script>
<script type="text/javascript" src="core/should.js"></script>
<script type="text/javascript" src="core/di.js"></script>
<script type="text/javascript" src="core/appShell.js"></script>
<script type="text/javascript" src="components/htmlBuilder.js"></script>
<script type="text/javascript" src="components/di.js"></script>
<script type="text/javascript" src="app/app.js"></script>
@@ -146,7 +147,9 @@
});
app.bootstrap(document.getElementById('rootView'));
app.component('dispatcher').dispatch('help');
app.get('dispatcher').dispatch('help');
app.get('dispatcher').dispatch('1|2');
app.get('dispatcher').dispatch('1 2');
</script>
</body>