增加对RESTFUL的支持, fix #82

This commit is contained in:
welefen 2014-07-07 14:34:53 +08:00
parent 05f5b9a5ae
commit dbe6bf44ca
4 changed files with 110 additions and 7 deletions

View File

@ -19,7 +19,7 @@ module.exports = {
post_max_fields: 1000, //最大表单数
post_max_fields_size: 2 * 1024, //单个表单最大值
app_group_list: ['Home', 'Admin'], //分组列表
app_group_list: ['Home', 'Admin', 'Restful'], //分组列表
default_group: 'Home', //默认分组
default_controller: 'Index', //默认模块
default_action: 'index', //默认Action
@ -37,6 +37,8 @@ module.exports = {
autoload_path: {}, //autoload查找的path用于thinkRequire加载自定义库的时候查找
create_server_fn: '', //自定义create server全局函数名可以在Common/common.js里实现
restful_group: 'Restful', //RESTFUL API默认分组
load_ext_config: [], //加载额外的配置文件 CONF_PATH
load_ext_file: [], //加载额外的文件 COMMON_PATH

View File

@ -142,7 +142,7 @@ module.exports = Behavior(function(){
* @param {[type]} route [description]
* @return {[type]} [description]
*/
getRoute: function(route){
getRoute: function(route, matches){
if (isObject(route)) {
//对应的请求类型
for(var method in route){
@ -153,6 +153,18 @@ module.exports = Behavior(function(){
}
return;
}
var routeUpper = route.toUpperCase();
//RESTFUL API
if (routeUpper === 'RESTFUL' || routeUpper.indexOf('RESTFUL:') === 0) {
var group = route.split(':')[1] || C('restful_group');
route = group + '/' + matches[1] + '/' + this.http.method.toLowerCase() + '?resource=' + matches[1];
if (matches[2]) {
route += '&id=' + matches[2];
}
//设置变量到http对象上方便后续使用
this.http.isRestful = true;
return route;
}
return route;
},
/**
@ -163,7 +175,7 @@ module.exports = Behavior(function(){
* @return {[type]} [description]
*/
parseRegExp: function(matches, route, pathname){
route = this.getRoute(route);
route = this.getRoute(route, matches);
if (!route) {
return false;
}

View File

@ -26,6 +26,10 @@ App.getBaseController = function(http){
*/
App.getCallController = function(http){
'use strict';
//如果是RESTFUL API则调用RestController
if (http.isRestful) {
return thinkRequire('RestController')(http);
}
var config = C('call_controller');
if (!config) {
return;

View File

@ -5,11 +5,96 @@
module.exports = Controller(function(){
'use strict';
return {
__before: function(){
init: function(http){
this.super('init', http);
//资源名
this.resource = this.get('resource');
//资源id
this.id = this.get('id') | 0;
//实例化对应的模型
this.model = D(this.resource);
},
__call: function(){
this.end('method is not allowed');
/**
* 获取
* @return {[type]} [description]
*/
getAction: function(){
var self = this;
if (this.id) {
return this.model.getPk().then(function(pk){
return self.model.where(getObject(pk, self.id)).find();
}).then(function(data){
return self.success(data);
}).catch(function(err){
return self.error(err.message);
})
}
return this.model.select().then(function(data){
return self.success(data);
}).catch(function(err){
return self.error(err.message);
});
},
/**
* 新建
* @return {[type]} [description]
*/
postAction: function(){
var self = this;
return this.model.getPk().then(function(pk){
var data = self.post();
if (isEmpty(data)) {
return self.error('data is empty');
}
delete data[pk];
return self.model.add(data);
}).then(function(){
return self.success();
}).catch(function(err){
return self.error(err.message);
});
},
/**
* 删除
* @return {[type]} [description]
*/
deleteAction: function(){
if (!this.id) {
return this.error('params error');
}
var self = this;
return this.model.getPk().then(function(pk){
return self.model.where(getObject(pk, self.id)).delete();
}).then(function(){
return self.success();
}).catch(function(err){
return self.error(err.message);
});
},
/**
* 更新
* @return {[type]} [description]
*/
putAction: function(){
if (!this.id) {
return this.error('params error');
}
var self = this;
return this.model.getPk().then(function(pk){
var data = self.post();
if (isEmpty(data)) {
return self.error('data is empty');
}
delete data[pk];
return self.model.where(getObject(pk, self.id)).update(data);
}).then(function(){
return self.success();
}).catch(function(err){
return self.error(err.message);
});
},
__call: function(action){
return this.error('action `' + action + '` is not allowed');
}
}
})