From d9ff0c3cf45a5f4e2f8d2a921c522ec6934aa50b Mon Sep 17 00:00:00 2001 From: Lars Jung Date: Sat, 13 Sep 2014 17:16:19 +0200 Subject: [PATCH] Clean code. --- src/_h5ai/client/js/inc/core/server.js | 78 ++++++++++++++------------ 1 file changed, 41 insertions(+), 37 deletions(-) diff --git a/src/_h5ai/client/js/inc/core/server.js b/src/_h5ai/client/js/inc/core/server.js index 861f7c58..ebeea8b6 100644 --- a/src/_h5ai/client/js/inc/core/server.js +++ b/src/_h5ai/client/js/inc/core/server.js @@ -1,52 +1,56 @@ modulejs.define('core/server', ['$', '_', 'config', 'core/location'], function ($, _, config, location) { - var server = { - backend: config.setup.BACKEND, - api: config.setup.API === true, - name: config.setup.SERVER_NAME, - version: config.setup.SERVER_VERSION, + var hasApi = config.setup.API === true; - request: function (data, callback) { - if (server.api) { - $.ajax({ - url: location.getAbsHref(), - data: data, - type: 'POST', - dataType: 'json' - }) - .done(function (json) { + function request(data, callback) { - callback(json); - }) - .fail(function () { + if (hasApi) { + $.ajax({ + url: location.getAbsHref(), + data: data, + type: 'POST', + dataType: 'json' + }) + .done(function (json) { + + callback(json); + }) + .fail(function () { - callback(); - }); - } else { callback(); - } - }, + }); + } else { + callback(); + } + } - formRequest: function (data) { + function formRequest(data) { - if (server.api) { - var $form = $('
') - .attr('action', location.getAbsHref()); + if (hasApi) { + var $form = $('') + .attr('action', location.getAbsHref()); - _.each(data, function (val, key) { + _.each(data, function (val, key) { - $('') - .attr('name', key) - .attr('value', val) - .appendTo($form); - }); + $('') + .attr('name', key) + .attr('value', val) + .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 + }; });