mirror of
https://github.com/kbparagua/paloma
synced 2023-03-27 23:21:17 -04:00
commit
0a5f180e8f
8 changed files with 94 additions and 113 deletions
|
@ -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(){
|
||||
|
|
|
@ -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(){
|
||||
|
||||
|
|
|
@ -1,10 +1,3 @@
|
|||
(function(Paloma){
|
||||
|
||||
var Controller = function(params){
|
||||
Paloma.Controller = function(params){
|
||||
this.params = params;
|
||||
};
|
||||
|
||||
|
||||
Paloma.Controller = Controller;
|
||||
|
||||
})(window.Paloma);
|
|
@ -1,13 +1,11 @@
|
|||
(function(Paloma){
|
||||
|
||||
|
||||
var ControllerFactory = function(router){
|
||||
Paloma.ControllerFactory = function(router){
|
||||
this.instances = {};
|
||||
this.router = router;
|
||||
};
|
||||
|
||||
Paloma.ControllerFactory.prototype = {
|
||||
|
||||
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);
|
||||
|
|
110
vendor/assets/javascripts/paloma/engine.js
vendored
110
vendor/assets/javascripts/paloma/engine.js
vendored
|
@ -1,45 +1,11 @@
|
|||
(function(Paloma){
|
||||
|
||||
var Engine = function(config){
|
||||
Paloma.Engine = function(config){
|
||||
this.factory = config.factory;
|
||||
this._clearRequest();
|
||||
};
|
||||
|
||||
Engine.prototype.start = function(){
|
||||
if ( !this._request ) return;
|
||||
if ( this._request.id == this.lastRequest().id ) return;
|
||||
Paloma.Engine.prototype = {
|
||||
|
||||
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();
|
||||
},
|
||||
|
||||
_executeActionOf: function(controller){
|
||||
var callback = controller[ this._request.action ];
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
Paloma.Engine = Engine;
|
||||
|
||||
})(window.Paloma);
|
||||
|
|
2
vendor/assets/javascripts/paloma/init.js
vendored
2
vendor/assets/javascripts/paloma/init.js
vendored
|
@ -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.");
|
||||
}
|
||||
}
|
||||
|
|
4
vendor/assets/javascripts/paloma/paloma.js
vendored
4
vendor/assets/javascripts/paloma/paloma.js
vendored
|
@ -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);
|
||||
|
|
22
vendor/assets/javascripts/paloma/router.js
vendored
22
vendor/assets/javascripts/paloma/router.js
vendored
|
@ -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,
|
||||
return {
|
||||
controllerPath: controllerPath,
|
||||
namespaces: namespaces,
|
||||
controller: controller};
|
||||
controller: controller
|
||||
};
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
Paloma.Router = Router;
|
||||
|
||||
})(window.Paloma);
|
Loading…
Reference in a new issue