Implemented controller as a mixin

This commit is contained in:
Borys Levytskyi
2015-04-03 11:50:38 +03:00
parent 47015a951c
commit ce2db72582
2 changed files with 29 additions and 11 deletions

View File

@@ -4,8 +4,6 @@
views: {} views: {}
}; };
var servicesContainer = {};
var controllersContainer = {};
var commandHandlers = {}; var commandHandlers = {};
var runObservers = []; var runObservers = [];
@@ -19,9 +17,16 @@
this.di.register(name, inst); this.di.register(name, inst);
}; };
app.get = function(name) {
return this.di.resolve(name);
};
app.service = app.component; app.service = app.component;
app.controller = app.component; app.controller = function(name, inst) {
this.addControllerMixin(inst);
this.di.register(name, inst);
};
app.command = function(name, handler) { app.command = function(name, handler) {
var cmd = commandHandlers[name]; var cmd = commandHandlers[name];
@@ -52,6 +57,25 @@
window.app = app; window.app = app;
app.addControllerMixin = function (component) {
component.attachView = function(viewElement) {
this.viewElement = viewElement;
if(typeof component.onViewAttached == 'function') {
component.onViewAttached(viewElement);
}
};
component.detachView = function() {
this.viewElement = null;
if(typeof component.onViewDetached == 'function') {
component.onViewDetached(viewElement);
}
};
}
})(window.should, window.commandr, window.bindr, window.Container); })(window.should, window.commandr, window.bindr, window.Container);

View File

@@ -2,9 +2,9 @@
app.controller('expressionInputCtrl', { app.controller('expressionInputCtrl', {
$dispatcher:null, $dispatcher:null,
attachView: function (viewElement) { onViewAttached: function () {
var d = this.$dispatcher; var d = this.$dispatcher;
viewElement.addEventListener('keyup', function (args) { this.viewElement.addEventListener('keyup', function (args) {
if (args.keyCode != 13) { if (args.keyCode != 13) {
return; return;
} }
@@ -17,12 +17,6 @@
}); });
app.service('resultView', { app.service('resultView', {
attachView: function(viewElement) {
this.viewElement = viewElement;
},
detachView: function() {
this.viewElement = null;
},
clear: function (){ clear: function (){
this.viewElement.innerHTML = ''; this.viewElement.innerHTML = '';
}, },