Enh: Added xhr to client response instances

Enh: Added response.header for receiving response header from xhr
Enh: Added post action to client module for data-action-click="client.post"
Fix: Try using options url as fallback in client calls if the action instance does not provide an url. e.g. client.post(evt, {url:...}
Enh: Enable setting ajax dataType from trigger e.g. data-action-click="modal.load" data-action-data-type="json"
This commit is contained in:
buddh4 2017-03-13 22:40:42 +01:00
parent 7cde220b15
commit 9b5abab9a0
2 changed files with 27 additions and 8 deletions

View File

@ -3,6 +3,11 @@ HumHub Change Log
1.2.0-beta.3 under developement
--------------------------------
- Enh: Added xhr to client response instances
- Enh: Added response.header for receiving response header from xhr
- Enh: Added post action to client module for data-action-click="client.post"
- Fix: Try using options url as fallback in client calls if the action instance does not provide an url. e.g. client.post(evt, {url:...}
- Enh: Enable setting ajax dataType from trigger e.g. data-action-click="modal.load" data-action-data-type="json"
- Enh: Added action event.data for receiving action specific data options
- Enh: Added default run for JsWidget
- Enh: Added File::findByRecord for searching all attached files of a given ActiveRecord

View File

@ -17,8 +17,9 @@ humhub.module('client', function (module, require, $) {
//Textstatus = "timeout", "error", "abort", "parsererror", "application"
this.textStatus = textStatus;
this.dataType = dataType;
this.xhr = xhr;
var responseType = xhr.getResponseHeader('content-type');
var responseType = this.header('content-type');
// If we expect json and received json we merge the json result with our response object.
if ((!dataType || dataType === 'json') && responseType && responseType.indexOf('json') > -1) {
@ -27,6 +28,10 @@ humhub.module('client', function (module, require, $) {
this[dataType] = this.response;
}
};
Response.prototype.header = function (key) {
return this.xhr.getResponseHeader(key);
};
Response.prototype.setSuccess = function (data) {
this.data = data;
@ -56,13 +61,11 @@ humhub.module('client', function (module, require, $) {
result.response = this.response.substr(0, 500)
result.response += (this.response.length > 500) ? '...' : '';
}
;
if (this.html && object.isString(this.html)) {
result.html = this.html.substr(0, 500)
result.html += (this.html.length > 500) ? '...' : '';
}
;
return result;
};
@ -73,7 +76,7 @@ humhub.module('client', function (module, require, $) {
} else {
location.reload(true);
}
}
};
var submit = function ($form, cfg, originalEvent) {
if ($form instanceof $.Event && $form.$form && $form.length) {
@ -99,11 +102,17 @@ humhub.module('client', function (module, require, $) {
var url = cfg.url || originalEvent.url || $form.attr('action');
return ajax(url, cfg, originalEvent);
};
var actionPost = function (evt) {
post(evt).catch(function(e) {
module.log.error(e, true);
});
};
var post = function (url, cfg, originalEvent) {
if (url instanceof $.Event) {
originalEvent = url;
url = originalEvent.url;
url = originalEvent.url || cfg.url;
} else if (cfg instanceof $.Event) {
originalEvent = cfg;
cfg = {};
@ -120,7 +129,7 @@ humhub.module('client', function (module, require, $) {
var html = function (url, cfg, originalEvent) {
if (url instanceof $.Event) {
originalEvent = url;
url = originalEvent.url;
url = originalEvent.url || cfg.url;
} else if (cfg instanceof $.Event) {
originalEvent = cfg;
cfg = {};
@ -138,7 +147,7 @@ humhub.module('client', function (module, require, $) {
var get = function (url, cfg, originalEvent) {
if (url instanceof $.Event) {
originalEvent = url;
url = originalEvent.url;
url = originalEvent.url || cfg.url;
} else if (cfg instanceof $.Event) {
originalEvent = cfg;
cfg = {};
@ -162,9 +171,13 @@ humhub.module('client', function (module, require, $) {
cfg = {'success': cfg};
}
var promise = new Promise(function (resolve, reject) {
cfg = cfg || {};
// allows data-action-data-type="json" on $trigger
if(originalEvent && object.isFunction(originalEvent.data)) {
cfg.dataType = originalEvent.data('data-type', cfg.dataType);
}
var errorHandler = cfg.error;
var error = function (xhr, textStatus, errorThrown) {
@ -285,6 +298,7 @@ humhub.module('client', function (module, require, $) {
module.export({
ajax: ajax,
actionPost: actionPost,
post: post,
get: get,
html: html,