您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

128 行
3.3 KiB

  1. // Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. //
  3. // MIT License (MIT)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a
  6. // copy of this software and associated documentation files (the "Software"),
  7. // to deal in the Software without restriction, including without limitation
  8. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. // and/or sell copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. //
  22. // My HTTP Request
  23. wn.provide('wn.request');
  24. wn.request.url = 'server.py';
  25. // call execute serverside request
  26. wn.request.prepare = function(opts) {
  27. // btn indicator
  28. if(opts.btn) $(opts.btn).set_working();
  29. // navbar indicator
  30. if(opts.show_spinner) set_loading();
  31. // freeze page
  32. if(opts.freeze) freeze();
  33. // no cmd?
  34. if(!opts.args.cmd) {
  35. console.log(opts)
  36. throw "Incomplete Request";
  37. }
  38. }
  39. wn.request.cleanup = function(opts, r) {
  40. // stop button indicator
  41. if(opts.btn) $(opts.btn).done_working();
  42. // hide button indicator
  43. if(opts.show_spinner) hide_loading();
  44. // un-freeze page
  45. if(opts.freeze) unfreeze();
  46. // session expired?
  47. if(wn.boot && wn.boot.sid && wn.get_cookie('sid') != wn.boot.sid) {
  48. if(!wn.app.logged_out) {
  49. msgprint('Session Expired. Logging you out');
  50. wn.app.logout();
  51. }
  52. return;
  53. }
  54. // show messages
  55. if(r.server_messages) msgprint(r.server_messages)
  56. // show errors
  57. if(r.exc) { console.log(r.exc); };
  58. // 403
  59. if(r['403']) {
  60. wn.container.change_to('403');
  61. }
  62. // sync docs
  63. if(r.docs) {
  64. LocalDB.sync(r.docs);
  65. }
  66. }
  67. wn.request.call = function(opts) {
  68. wn.request.prepare(opts);
  69. $.ajax({
  70. url: opts.url || wn.request.url,
  71. data: opts.args,
  72. type: opts.type || 'POST',
  73. dataType: opts.dataType || 'json',
  74. success: function(r, xhr) {
  75. wn.request.cleanup(opts, r);
  76. opts.success(r, xhr.responseText);
  77. },
  78. error: function(xhr, textStatus) {
  79. wn.request.cleanup(opts, {});
  80. show_alert('Unable to complete request: ' + textStatus)
  81. if(opts.error)opts.error(xhr)
  82. }
  83. })
  84. }
  85. // generic server call (call page, object)
  86. wn.call = function(opts) {
  87. var args = $.extend({}, opts.args)
  88. // cmd
  89. if(opts.module && opts.page) {
  90. args.cmd = opts.module+'.page.'+opts.page+'.'+opts.page+'.'+opts.method
  91. } else if(opts.method) {
  92. args.cmd = opts.method;
  93. }
  94. // stringify args if required
  95. for(key in args) {
  96. if(args[key] && typeof args[key] != 'string') {
  97. args[key] = JSON.stringify(args[key]);
  98. }
  99. }
  100. wn.request.call({
  101. args: args,
  102. success: opts.callback,
  103. error: opts.error,
  104. btn: opts.btn,
  105. freeze: opts.freeze,
  106. show_spinner: !opts.no_spinner
  107. });
  108. }