Comparing version 0.8.8 to 0.9.0
@@ -16,6 +16,31 @@ /** | ||
var Controller = module.exports = function(){ | ||
this.context = icecream; | ||
if(this.context.shareObject){ | ||
utils.merge(this, this.context.shareObject); | ||
var self = this; | ||
self.context = icecream; | ||
self.actions = { | ||
GET:{}, | ||
POST:{}, | ||
HEAD:{}, | ||
TRACE:{}, | ||
PUT:{}, | ||
DELETE:{}, | ||
OPTIONS:{}, | ||
CONNECT:{}, | ||
"ACTION":{} | ||
}; | ||
self.actionLoaders = { | ||
self : this, | ||
action : function(name, func){ | ||
self.actions['ACTION'][name] = func; | ||
}, | ||
beforeFilter : function(func){ | ||
self.actions.beforeFilter = func; | ||
}, | ||
afterFilter : function(func){ | ||
self.actions.afterFilter = func; | ||
} | ||
} | ||
if(self.context.shareObject){ | ||
utils.merge(self, self.context.shareObject); | ||
} | ||
}; | ||
@@ -25,2 +50,6 @@ | ||
prototype.getAction = function(method, action){ | ||
return this.actions[method][action]; | ||
} | ||
prototype.render = function(){ | ||
@@ -210,13 +239,1 @@ | ||
} | ||
prototype.action = function(name, func){ | ||
this[name] = func; | ||
} | ||
prototype.beforeFilter = function(func){ | ||
this.beforeFilter = func; | ||
} | ||
prototype.afterFilter = function(func){ | ||
this.afterFilter = func; | ||
} |
@@ -7,3 +7,3 @@ /** | ||
* http://github.com/zhiyu/icecream | ||
* | ||
* | ||
*/ | ||
@@ -24,3 +24,3 @@ | ||
dispatcher.dispatch = function(req, res){ | ||
if(this.context.get("static") && this.context.getObject("statics", req.url) && fs.existsSync(this.context.get('staticDir')+this.context.getObject("statics", req.url))){ | ||
@@ -35,3 +35,3 @@ fs.readFile(this.context.get('staticDir')+this.context.getObject("statics", req.url), "binary", function(err, data) { | ||
}); | ||
}else{ | ||
}else{ | ||
if(this.isAction(req)){ | ||
@@ -51,3 +51,2 @@ this.doAction(req,res); | ||
dispatcher.doAction = function(req,res){ | ||
res.setHeader("Content-Type", "text/html; charset=" + this.context.get("encoding")); | ||
var errMessage = null; | ||
@@ -63,3 +62,3 @@ | ||
var route = this.context.getObject("routes", url); | ||
var route = this.context.getObject("routes", url); | ||
if(route){ | ||
@@ -71,3 +70,3 @@ url = route; | ||
var action = this.getAction(url); | ||
//if no controller | ||
@@ -80,3 +79,3 @@ if(controller == null){ | ||
} | ||
//set variables | ||
@@ -89,13 +88,16 @@ controller.context = this.context; | ||
if(controller[action]){ | ||
res.setHeader("Content-Type", "text/html; charset=" + this.context.get("encoding")); | ||
var act = controller.getAction('ACTION', action); | ||
if(act){ | ||
//beforeFileter | ||
if(controller['beforeFilter']) | ||
controller['beforeFilter'](); | ||
if(controller.actions.beforeFilter) | ||
controller.actions.beforeFilter.call(controller); | ||
//action | ||
controller[action].call(controller); | ||
act.call(controller); | ||
//afterFileter | ||
if(controller['afterFilter']) | ||
controller['afterFilter'](); | ||
if(controller.actions.afterFilter) | ||
controller.actions.afterFilter.call(controller); | ||
}else{ | ||
@@ -110,3 +112,3 @@ errMessage = 'action "'+ action + '" not exist!'; | ||
var url = req._parsedUrl.pathname; | ||
var vpath = this.context.get('vpath'); | ||
@@ -128,3 +130,3 @@ if(vpath!='' && url.indexOf(vpath) == 0){ | ||
var lastModified = stat.mtime.toUTCString(); | ||
if (req.headers['if-modified-since'] && lastModified == req.headers['if-modified-since']) { | ||
@@ -142,3 +144,3 @@ res.writeHead(304, "Not Modified"); | ||
} | ||
res.writeHead(status, {'Last-Modified':lastModified,'Content-Type': contentType }); | ||
@@ -164,3 +166,3 @@ res.write(content, "binary"); | ||
var absName = this.context.get('appDir')+'/controllers' + relName; | ||
if(!fs.existsSync(absName)){ | ||
@@ -175,3 +177,3 @@ return null; | ||
var content = fs.readFileSync(absName).toString(); | ||
new Function('context', 'require','with(context){'+ content + '}')(controller,require); | ||
new Function('context', 'require','with(context){'+ content + '}')(controller.actionLoaders,require); | ||
this.context.setObject("controllers", absName, controller); | ||
@@ -228,2 +230,2 @@ }else{ | ||
return this.context.get('debug')==true || this.context.getObject("controllers", file) == undefined; | ||
} | ||
} |
@@ -53,3 +53,3 @@ { | ||
}, | ||
"version": "0.8.8" | ||
"version": "0.9.0" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
728483
17723