e91d1b9f28b7ce7ef6040c4c90033ae315573157.svn-base 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. (function () {
  2. var endpointSelector = new DevExpress.EndpointSelector(Partials.config.endpoints);
  3. function handleServiceError(error) {
  4. if (window.WinJS) {
  5. try {
  6. new Windows.UI.Popups.MessageDialog(error.message).showAsync();
  7. } catch (e) {
  8. }
  9. } else {
  10. alert(error.message);
  11. }
  12. }
  13. $.support.cors = true;
  14. Partials.api = Partials.api = {
  15. url: endpointSelector.urlFor("api"),
  16. errorHandler: handleServiceError,
  17. post: function (apiName, postData, parameters) {
  18. var url = this.makeApiUriWithParameters(apiName, parameters);
  19. var promise = $.ajax({
  20. url: url,
  21. dataType: 'json',
  22. contentType: 'application/json',
  23. method: "POST",
  24. data: JSON.stringify(postData)
  25. });
  26. promise.fail(handleServiceError);
  27. return promise;
  28. },
  29. get: function (apiName, parameters) {
  30. var url = this.makeApiUriWithParameters(apiName, parameters);
  31. var promise = $.ajax({
  32. url: url,
  33. method: "GET",
  34. });
  35. promise.fail(handleServiceError);
  36. return promise;
  37. },
  38. makeApiUriWithParameters: function (apiName, parameters) {
  39. var uri = [this.url, '/'];
  40. uri.push(apiName);
  41. if (typeof parameters === 'object') {
  42. uri.push('?');
  43. $.each(parameters, function (name, value) {
  44. if (value == null) return true;
  45. uri.push('&');
  46. uri.push(name);
  47. uri.push('=');
  48. uri.push(value);
  49. });
  50. }
  51. return uri.join('');
  52. },
  53. }
  54. }());