UserProNetMediaZaliczka.php.fetchData.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // @require var SYNC_URL
  2. function zaliczkaFetchData(query, args, resultCallback) {
  3. console.warn('TODO: zaliczkaFetchData(query, objectNamespace, resultCallback) query('+query+') args:', args);
  4. // objectNamespace: Object {"@namespace": "default_db:IN7_DZIENNIK_KORESP", id: "xsd:integer", title: "xsd:string", kategoriaKosztu: "xsd:string"}
  5. if ('object' !== typeof args) {
  6. console.log("BUG: Unsupported type '" + args + "'");
  7. return;
  8. }
  9. if (!args['@namespace'] && !args['objectNamespace']) {
  10. // TODO: log error
  11. console.error("BUG: Missing namespace in args");
  12. return;
  13. }
  14. if (args['@namespace']) {
  15. switch (args['@namespace']) {
  16. case 'default_db/IN7_MK_BAZA_DYSTRYBUCJI/ZaliczkaProjekt':
  17. case 'default_db/IN7_DZIENNIK_KORESP/ZaliczkaKoresp':
  18. {
  19. superagent
  20. .post(SYNC_URL)
  21. .type('json') // header ĺapplication/x-www-form-urlencoded' requires type('form');
  22. .send({
  23. schema: args,
  24. query: query
  25. })
  26. .set('Accept', 'application/json')
  27. .end(function(err, res) {
  28. var payload = (err || !res.ok || 'application/json' !== res.type)
  29. ? {type: 'warning', msg: (res.body && res.body.msg) ? res.body.msg : 'Request error', body: res.body}
  30. : {type: 'success', msg: res.body.msg || '', body: res.body}
  31. jQuery(document).trigger('DBG:notify', payload)
  32. console.log('F.fetchDataAjax res.body', res.body)
  33. console.log('F.fetchDataAjax payload', payload)
  34. jQuery.notify(payload.msg, payload.type)
  35. resultCallback(payload.body)
  36. });
  37. }
  38. break;
  39. default: {
  40. console.log("BUG: Unsupported type namespace '" + args['@namespace'] + "'");
  41. }
  42. }
  43. return;
  44. }
  45. if ('default_db/ZALICZKA_POZYCJA/ZaliczkaPozycja' == args['objectNamespace']) {
  46. switch (args['childName']) {
  47. case 'typ_dokumentu':
  48. case 'kategoria_kosztu': {
  49. fetch(SYNC_URL, {
  50. method: 'POST',
  51. credentials: 'same-origin',// add cookies
  52. headers: { 'Content-Type': 'application/json' },
  53. body: JSON.stringify({
  54. objectNamespace: args['objectNamespace'],
  55. childName: args['childName'],
  56. query: query
  57. })
  58. }).then(function (response) {
  59. return response.json()
  60. }).then(function (data) {
  61. if (!data) throw "Empty response";
  62. console.log('loadDataAjax:fetch: request finished data:', data);
  63. if ('error' === data.type) {
  64. p5UI__notifyAjaxCallback(data);
  65. console.log('loadDataAjax:fetch: ERR:', e);
  66. } else if ('success' === data.type) {
  67. resultCallback({options: data.options || []});
  68. } else {
  69. p5UI__notifyAjaxCallback(data);
  70. console.log('loadDataAjax:fetch: ERR:', e);
  71. }
  72. }).catch(function (e) {
  73. p5UI__notifyAjaxCallback({
  74. type: 'error',
  75. msg: "Request error " + e
  76. });
  77. console.log('loadDataAjax:fetch: ERR:', e);
  78. });
  79. } break;
  80. default: {
  81. console.warn("BUG: Unsupported namespace '" + args['objectNamespace'] + "'");
  82. }
  83. }
  84. return;
  85. }
  86. }
  87. global.zaliczkaFetchData = zaliczkaFetchData