Clean code.

This commit is contained in:
Lars Jung 2014-09-13 17:16:19 +02:00
parent d9d5e9473d
commit d9ff0c3cf4

View file

@ -1,52 +1,56 @@
modulejs.define('core/server', ['$', '_', 'config', 'core/location'], function ($, _, config, location) { modulejs.define('core/server', ['$', '_', 'config', 'core/location'], function ($, _, config, location) {
var server = {
backend: config.setup.BACKEND, var hasApi = config.setup.API === true;
api: config.setup.API === true,
name: config.setup.SERVER_NAME,
version: config.setup.SERVER_VERSION,
request: function (data, callback) {
if (server.api) { function request(data, callback) {
$.ajax({
url: location.getAbsHref(),
data: data,
type: 'POST',
dataType: 'json'
})
.done(function (json) {
callback(json); if (hasApi) {
}) $.ajax({
.fail(function () { url: location.getAbsHref(),
data: data,
type: 'POST',
dataType: 'json'
})
.done(function (json) {
callback(json);
})
.fail(function () {
callback();
});
} else {
callback(); callback();
} });
}, } else {
callback();
}
}
formRequest: function (data) { function formRequest(data) {
if (server.api) { if (hasApi) {
var $form = $('<form method="post" style="display:none;"/>') var $form = $('<form method="post" style="display:none;"/>')
.attr('action', location.getAbsHref()); .attr('action', location.getAbsHref());
_.each(data, function (val, key) { _.each(data, function (val, key) {
$('<input type="hidden"/>') $('<input type="hidden"/>')
.attr('name', key) .attr('name', key)
.attr('value', val) .attr('value', val)
.appendTo($form); .appendTo($form);
}); });
$form.appendTo('body').submit().remove(); $form.appendTo('body').submit().remove();
} }
} }
};
return server;
return {
backend: config.setup.BACKEND,
api: hasApi,
name: config.setup.SERVER_NAME,
version: config.setup.SERVER_VERSION,
request: request,
formRequest: formRequest
};
}); });