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

Merge pull request #88 from kbparagua/refactor

Refactor
This commit is contained in:
Karl Bryan Paragua 2016-03-03 00:11:09 +08:00
commit 0a5f180e8f
8 changed files with 94 additions and 113 deletions

View file

@ -1,7 +1,7 @@
describe('Paloma.ControllerFactory', function(){
var router = new Paloma.Router({namespace: '/', action: '#'});
var router = new Paloma.Router({namespaceDelimiter: '/'});
describe('#make(name)', function(){
@ -45,4 +45,4 @@ describe('Paloma.ControllerFactory', function(){
});
});
});

View file

@ -1,7 +1,7 @@
describe('Paloma.Router', function(){
var delimiter = '/',
router = new Paloma.Router(delimiter);
router = new Paloma.Router({namespaceDelimiter: delimiter});
describe('#parse(path)', function(){
@ -49,4 +49,4 @@ describe('Paloma.Router', function(){
});
});
});

View file

@ -1,10 +1,3 @@
(function(Paloma){
var Controller = function(params){
this.params = params;
};
Paloma.Controller = Controller;
})(window.Paloma);
Paloma.Controller = function(params){
this.params = params;
};

View file

@ -1,13 +1,11 @@
(function(Paloma){
Paloma.ControllerFactory = function(router){
this.instances = {};
this.router = router;
};
Paloma.ControllerFactory.prototype = {
var ControllerFactory = function(router){
this.instances = {};
this.router = router;
};
ControllerFactory.prototype.make = function(name){
make: function(name){
var config = this.router.parse(name),
scope = this.instances;
@ -18,11 +16,10 @@
scope = scope[namespace];
}
return scope[config['controller']] = createConstructor();
};
return scope[config['controller']] = this._createConstructor();
},
ControllerFactory.prototype.get = function(name){
get: function(name){
var config = this.router.parse(name),
scope = this.instances;
@ -34,19 +31,14 @@
}
return scope;
};
},
_createConstructor: function(){
var constructor = function(params){ Paloma.Controller.call(this, params); };
var createConstructor = function(){
var constructor = function(params){ this.params = params; }
constructor.prototype.__proto__ = Paloma.Controller.prototype;
return constructor;
};
}
Paloma.ControllerFactory = ControllerFactory;
})(window.Paloma);
};

View file

@ -1,45 +1,11 @@
(function(Paloma){
Paloma.Engine = function(config){
this.factory = config.factory;
this._clearRequest();
};
var Engine = function(config){
this.factory = config.factory;
this._clearRequest();
};
Paloma.Engine.prototype = {
Engine.prototype.start = function(){
if ( !this._request ) return;
if ( this._request.id == this.lastRequest().id ) return;
this._lastRequest = this._request;
var resource = this._request.controller,
action = this._request.action,
params = this._request.params;
var Controller = this.factory.get(resource),
controller = null;
Paloma.log('Paloma: ' + resource + '#' + action + ' with params:');
Paloma.log(params);
if ( !Controller ) return true;
controller = new Controller(params);
if ( !controller[action] ) return true;
controller[action]();
this._lastRequest.executed = true;
this._clearRequest();
};
//
// options:
// resource
// action
// params
// id
//
Engine.prototype.setRequest = function(options){
setRequest: function(options){
this._request = {
id: options.id,
controller: options.resource,
@ -47,30 +13,58 @@
params: options.params,
executed: false
};
};
},
Engine.prototype.getRequest = function(key){
return (!key ? this._request : this._request[key]);
};
Engine.prototype.lastRequest = function(){
return this._lastRequest || {executed: false};
};
Engine.prototype.hasRequest = function(){
hasRequest: function(){
return this._request != null;
};
},
Engine.prototype._clearRequest = function(){
this._request = null;
};
lastRequest: function(){
return this._lastRequest = this._lastRequest || {executed: false};
},
start: function(){
if ( this._shouldStop() ) return;
this._logRequest();
this._lastRequest = this._request;
var controllerClass = this.factory.get( this._request.controller );
if (controllerClass){
var controller = new controllerClass( this._request.params );
this._executeActionOf(controller);
}
Engine.prototype._stopWithWarning = function(warning){
Paloma.warn(warning);
this._clearRequest();
};
},
Paloma.Engine = Engine;
_executeActionOf: function(controller){
var callback = controller[ this._request.action ];
})(window.Paloma);
if (callback){
callback.call(controller);
this._lastRequest.executed = true;
}
},
_shouldStop: function(){
if ( !this.hasRequest() ) return true;
if ( this._request.id == this.lastRequest().id ) return true;
return false;
},
_logRequest: function(){
Paloma.log(
'Paloma: ' + this._request.controller + '#' +
this._request.action + ' with params:'
);
Paloma.log( this._request.params );
},
_clearRequest: function(){
this._request = null;
}
};

View file

@ -20,7 +20,7 @@ else {
if ( !window['Paloma'] ){
if (window['console'] !== undefined){
if ( !window['console'] ){
console.warn("Paloma not found. Require it in your application.js.");
}
}

View file

@ -1,6 +1,6 @@
(function(Paloma){
Paloma._router = new Paloma.Router('/');
Paloma._router = new Paloma.Router({namespaceDelimiter: '/'});
Paloma._controllerFactory = new Paloma.ControllerFactory(Paloma._router);
//
@ -30,6 +30,4 @@
return this.engine.lastRequest().executed;
};
})(window.Paloma);

View file

@ -1,23 +1,27 @@
(function(Paloma){
Paloma.Router = function(options){
options = options || {};
this.namespaceDelimiter = options.namespaceDelimiter;
var Router = function(namespaceDelimiter){
this.namespaceDelimiter = namespaceDelimiter;
};
if (!this.namespaceDelimiter)
throw "Paloma.Router: namespaceDelimiter option is required.";
};
Paloma.Router.prototype = {
Router.prototype.parse = function(path){
parse: function(path){
var parts = path.split(this.namespaceDelimiter),
controller = parts.pop(),
namespaces = parts;
var controllerPath = namespaces.concat([controller]);
return {controllerPath: controllerPath,
namespaces: namespaces,
controller: controller};
};
return {
controllerPath: controllerPath,
namespaces: namespaces,
controller: controller
};
}
};
Paloma.Router = Router;
})(window.Paloma);