osr
Version:
osr
78 lines (65 loc) • 2.02 kB
JavaScript
var fs = require("fs");
var promise = require("promise");
var OSR = function(){
this._maps = {};
this._config = {};
this._models = {};
this.daoList = [];
this._routes = {};
this._controllers = {};
}
OSR.prototype.promise = promise;
OSR.prototype.get = function(key){
return this._maps[key];
}
OSR.prototype.import = function(path){
var files = fs.readdirSync(path);
files.forEach(function(item,index){
require(path+"/"+item);
})
}
OSR.prototype.set = function(key,value){
return this._maps[key] = value;
}
OSR.prototype.Dao = require("./core/dao");
OSR.prototype.Model = require("./core/model");
OSR.prototype.Controller = require("./core/controller");
OSR.prototype.Route = require("./core/route");
OSR.prototype.getModel = function(name){
return this._models[name];
}
OSR.prototype.run = function(app){
this.app = app;
for(var key in this._routes){
var route = this._routes[key];
for(var sub in route.routes){
route.routes[sub].forEach(function(item,index){
app[sub](item.path,item.fn);
});
}
}
}
OSR.prototype.emit = function(name){
var _this = this;
var _arguments = arguments;
var _promise = new this.promise(function(resolve,reject){
var _names = name.split("::");
var controller = _names[0];
var method = _names[1];
if(!_this._controllers[controller]){
throw new Error("controller::"+controller+" not found");
}
if(!_this._controllers[controller][method]){
throw new Error("controller->method::"+controller+"->"+method+" not found");
}
resolve(_this._controllers[controller][method].apply(_arguments,[].slice.call(_arguments,1)));
})
return _promise;
}
var _osr = null;
module.exports = exports = (function(){
if(!_osr){
_osr = new OSR();
}
return _osr;
})();