script.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * Steps handler
  3. */
  4. var Steps = {}
  5. Steps.init = function() {
  6. this.buildParseUrl();
  7. this.bindBtn('#step-1-btn', function(e){
  8. ParseRequest.postData();
  9. e.preventDefault();
  10. })
  11. }
  12. Steps.buildParseUrl = function() {
  13. var url = Config.getUrl();
  14. $('#parse-url').html(url + '/parse');
  15. }
  16. Steps.bindBtn = function(id, callback) {
  17. $(id).click(callback)
  18. }
  19. Steps.closeStep = function(id) {
  20. $(id).addClass('step--disabled');
  21. }
  22. Steps.openStep = function(id) {
  23. $(id).removeClass('step--disabled');
  24. }
  25. Steps.fillStepOutput = function(id, data) {
  26. $(id).html('Output: ' + data).slideDown();
  27. }
  28. Steps.fillBtn = function(id, message) {
  29. $(id).addClass('success').html('✓ ' + message);
  30. }
  31. Steps.showWorkingMessage = function() {
  32. $('#step-4').delay(500).slideDown();
  33. }
  34. /**
  35. * Parse requests handler
  36. */
  37. var ParseRequest = {};
  38. ParseRequest.postData = function() {
  39. XHR.setCallback(function(data){
  40. // store objectID
  41. Store.objectId = JSON.parse(data).objectId;
  42. // close first step
  43. Steps.closeStep('#step-1');
  44. Steps.fillStepOutput('#step-1-output', data)
  45. Steps.fillBtn('#step-1-btn', 'Posted');
  46. // open second step
  47. Steps.openStep('#step-2');
  48. Steps.bindBtn('#step-2-btn', function(e){
  49. ParseRequest.getData();
  50. e.preventDefault();
  51. });
  52. });
  53. XHR.POST('/parse/classes/GameScore');
  54. }
  55. ParseRequest.getData = function() {
  56. XHR.setCallback(function(data){
  57. // close second step
  58. Steps.closeStep('#step-2');
  59. Steps.fillStepOutput('#step-2-output', data)
  60. Steps.fillBtn('#step-2-btn', 'Fetched');
  61. // open third step
  62. Steps.openStep('#step-3');
  63. Steps.bindBtn('#step-3-btn', function(e){
  64. ParseRequest.postCloudCodeData();
  65. e.preventDefault();
  66. })
  67. });
  68. XHR.GET('/parse/classes/GameScore');
  69. }
  70. ParseRequest.postCloudCodeData = function() {
  71. XHR.setCallback(function(data){
  72. // close second step
  73. Steps.closeStep('#step-3');
  74. Steps.fillStepOutput('#step-3-output', data)
  75. Steps.fillBtn('#step-3-btn', 'Tested');
  76. // open third step
  77. Steps.showWorkingMessage();
  78. });
  79. XHR.POST('/parse/functions/hello');
  80. }
  81. /**
  82. * Store objectId and other references
  83. */
  84. var Store = {
  85. objectId: ""
  86. };
  87. var Config = {}
  88. Config.getUrl = function() {
  89. if (url) return url;
  90. var port = window.location.port;
  91. var url = window.location.protocol + '//' + window.location.hostname;
  92. if (port) url = url + ':' + port;
  93. return url;
  94. }
  95. /**
  96. * XHR object
  97. */
  98. var XHR = {}
  99. XHR.setCallback = function(callback) {
  100. this.xhttp = new XMLHttpRequest();
  101. var _self = this;
  102. this.xhttp.onreadystatechange = function() {
  103. if (_self.xhttp.readyState == 4 && _self.xhttp.status >= 200 && _self.xhttp.status <= 299) {
  104. callback(_self.xhttp.responseText);
  105. }
  106. };
  107. }
  108. XHR.POST = function(path, callback) {
  109. var seed = {"score":1337,"playerName":"Sean Plott","cheatMode":false}
  110. this.xhttp.open("POST", Config.getUrl() + path, true);
  111. this.xhttp.setRequestHeader("X-Parse-Application-Id", "myAppId");
  112. this.xhttp.setRequestHeader("Content-type", "application/json");
  113. this.xhttp.send(JSON.stringify(seed));
  114. }
  115. XHR.GET = function(path, callback) {
  116. this.xhttp.open("GET", Config.getUrl() + path + '/' + Store.objectId, true);
  117. this.xhttp.setRequestHeader("X-Parse-Application-Id", "myAppId");
  118. this.xhttp.setRequestHeader("Content-type", "application/json");
  119. this.xhttp.send(null);
  120. }
  121. /**
  122. * Boot
  123. */
  124. Steps.init();