1
0
Fork 0
mirror of https://github.com/kbparagua/paloma synced 2023-03-27 23:21:17 -04:00

adjust Paloma.Engine to use new ControllerBuilder

This commit is contained in:
kbparagua 2016-03-24 22:57:07 +08:00
parent 2bf55339ec
commit 13f07269a1
4 changed files with 67 additions and 22 deletions

View file

@ -0,0 +1,34 @@
Paloma.ControllerBuilder = function(classFactory){
this.classFactory = classFactory;
this.options = {};
};
Paloma.ControllerBuilder.prototype = {
build: function(options){
this.options = options;
var ControllerClass = this._controllerClass(),
if ( !ControllerClass ) return null;
return new ControllerClass( this._buildParams() );
},
_controllerClass: function(){
return this.classFactory.get( this.options.controller );
},
_buildParams: function(){
var params = {
_controller: this.options.controller,
_action: this.options.action
};
for (var k in this.options.params)
if (this.options.hasOwnProperty(k))
params[k] = this.options.params[k];
return params;
}
};

View file

@ -1,5 +1,5 @@
Paloma.Engine = function(config){
this.builder = config.builder;
Paloma.Engine = function(controllerBuilder){
this.controllerBuilder = controllerBuilder;
this._clearRequest();
};
@ -29,27 +29,31 @@ Paloma.Engine.prototype = {
this._logRequest();
this._lastRequest = this._request;
var controllerClass = this.builder.get( this._request.controller );
if (controllerClass){
var controller = new controllerClass( this._request.params );
this._executeActionOf(controller);
}
this._executeControllerAction();
this._clearRequest();
},
_executeActionOf: function(controller){
_executeControllerAction: function(){
var controller = this._buildController();
if (!controller) return;
var action = controller[ this._request.action ];
if (!action) return;
if (action){
var callbackPerformer = new Paloma.BeforeCallbackPerformer(controller);
callbackPerformer.perform( this._request.action );
var callbackPerformer = new Paloma.BeforeCallbackPerformer(controller);
callbackPerformer.perform( this._request.action );
action.call(controller);
action.call(controller);
this._lastRequest.executed = true;
}
this._lastRequest.executed = true;
},
_buildController: function(){
return this.controllerBuilder.build({
controller: this._request.controller,
action: this._request.action,
params: this._request.params
});
},
_shouldStop: function(){
@ -71,4 +75,5 @@ Paloma.Engine.prototype = {
_clearRequest: function(){
this._request = null;
}
};

View file

@ -2,5 +2,6 @@
//= require ./base_controller.js
//= require ./controller_class_factory.js
//= require ./before_callback_performer.js
//= require ./controller_builder.js
//= require ./engine.js
//= require ./paloma.js

View file

@ -1,10 +1,15 @@
(function(Paloma){
Paloma._controllerClassFactory = new Paloma.ControllerClassFactory();
Paloma.engine = new Paloma.Engine({builder: Paloma._controllerBuilder});
var classFactory = new Paloma.ControllerClassFactory(),
controllerBuilder = new Paloma.ControllerBuilder(classFactory),
engine = new Paloma.Engine(controllerBuilder)
Paloma._controllerClassFactory = classFactory;
Paloma._controllerBuilder = controllerBuilder
Paloma.engine = engine;
Paloma.controller = function(name, prototype){
return Paloma._controllerClassFactory.make(name, prototype);
return classFactory.make(name, prototype);
};
Paloma._executeHook = function(){
@ -13,12 +18,12 @@
};
Paloma.start = function(){
if ( !this.engine.hasRequest() ) this._executeHook();
if ( this.engine.hasRequest() ) this.engine.start();
if ( !engine.hasRequest() ) this._executeHook();
if ( engine.hasRequest() ) engine.start();
};
Paloma.isExecuted = function(){
return this.engine.lastRequest().executed;
return engine.lastRequest().executed;
};
})(window.Paloma);