##// END OF EJS Templates
merge
merge

File last commit:

r0:7110eac54b19 v1.0.0 default
r3:e9671275b348 merge tip default
Show More
Resource.js
128 lines | 3.6 KiB | application/javascript | JavascriptLexer
cin
initial port of implab/web
r0 define([
"dojo/_base/declare",
"dojo/_base/lang",
"dojo/when",
"implab/safe",
"./NotAllowedException",
"./HttpResponse"
], function (
declare,
lang,
when,
safe,
NotAllowedException,
HttpResponse
) {
let resource = declare(null, {
// instance members
request: null,
// родительский ресурс
parent: null,
// имя текущего ресурса, является фрагментом пути к запрашиваемому
// ресурсу
name: null,
allowedMethods: {
"head": 0,
"options": 0,
"get": 0,
"post": 0,
"put": 1, // extended method
"delete": 1 // extended method
},
constructor: function (options) {
if (options) {
declare.safeMixin(this, options);
}
},
accessCheck: null,
getAllowedMethods: function (cors) {
let methods = [];
for (var m in this.allowedMethods) {
if (m in this && (cors && this.allowedMethods[m] || !cors))
methods.push(m.toUpperCase());
}
return methods;
},
options: function () {
let resp = new HttpResponse(null, {
headers: {
"Access-Control-Allow-Methods": this.getAllowedMethods(true),
"Allowed-Methods": this.getAllowedMethods()
}
});
return resp;
},
invoke: function () {
let method = this.request.method.toLowerCase();
let me = this;
if (!(method in this)) {
throw new NotAllowedException(); // TODO: Возвратить список возможных методов (verbs)
}
if (!safe.isNull(this.accessCheck)) {
return when(this.request.session, function (session) {
me.session = session;
return when(me.accessCheck(), function () {
return me[method]();
});
}, function (err) {
console.log(err);
throw err;
});
} else {
return this[method]();
}
},
render: function (view, model, mimeType) {
return function (resp) {
if (mimeType)
resp.type(mimeType);
resp.render(view, {
model: model
});
};
},
getChild: function (name) {
if (this.children && name in this.children) {
let child = this.children[name];
if (typeof child == "function") {
return child({
request: this.request,
parent: this,
name: name
});
} else if (child.hasOwnProperty("isInstanceOf") && child.isInstanceOf(resource)) {
return lang.mixin(child, {
request: this.request,
parent: this,
name: name
});
} else {
return new resource(lang.mixin(child, {
request: this.request,
parent: this,
name: name
}));
}
} else {
return null;
}
}
});
return resource;
});