koify.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. var koify = (function($, undefined) {
  17. function KOClass(fn, attrs) {
  18. var _cclass = cclass.create(fn, attrs);
  19. function reset_extends(fn, cclass) {
  20. cclass._extend = cclass.extend;
  21. cclass.extend = function() {
  22. var args = [fn];
  23. args.push.apply(args, arguments);
  24. var _fn = reset_extends(fn, cclass._extend.apply(cclass, args));
  25. return _fn;
  26. }
  27. return cclass;
  28. }
  29. return reset_extends(fn, _cclass);
  30. }
  31. var Model = KOClass(function(attrs) {
  32. var self = this;
  33. var attrs = attrs || {};
  34. $.extend(self, self.initialize(attrs));
  35. $.extend(self, self); // Add attributes in prototype chain
  36. }, {
  37. initialize: function(attrs) {
  38. return attrs;
  39. }
  40. });
  41. var MinimalNode = KOClass(function(options) {
  42. var self = this;
  43. var options = options || {};
  44. self.options = options;
  45. self.initialize(options);
  46. }, {
  47. model_class: undefined,
  48. initialize: function(options) {
  49. var self = this;
  50. if (options.modelDict) {
  51. self.model = new self.model_class(options.modelDict);
  52. } else {
  53. self.model = $.extend(true, {}, options.model);
  54. }
  55. self.map();
  56. },
  57. map: function() {
  58. var self = this;
  59. var mapping_options = {
  60. 'ignore': ['parent', 'initialize']
  61. };
  62. if ('__ko_mapping__' in self) {
  63. ko.mapping.fromJS(self.model, mapping_options, self);
  64. } else {
  65. var mapped = ko.mapping.fromJS(self.model, mapping_options);
  66. $.extend(self, mapped);
  67. }
  68. }
  69. });
  70. var Node = MinimalNode.extend({
  71. persist: true,
  72. identifier: undefined,
  73. request: function(url, options) {
  74. var self = this;
  75. var request = $.extend({
  76. url: url,
  77. dataType: 'json',
  78. type: 'GET',
  79. success: $.noop,
  80. error: $.noop
  81. }, options || {});
  82. $.ajax(request);
  83. },
  84. load: function(options) {
  85. var self = this;
  86. $(document).trigger('load.' + self.identifier, [self, options]);
  87. var options = $.extend({
  88. success: function(data) {
  89. switch(data.status) {
  90. case 0:
  91. self.initialize({modelDict: data[self.identifier]});
  92. $(document).trigger('loaded.' + self.identifier, [self, options]);
  93. break;
  94. default:
  95. case 1:
  96. $(document).trigger('load_fail.' + self.identifier, [self, options, data]);
  97. break;
  98. }
  99. }
  100. }, options);
  101. self.request(self.loadUrl(), options);
  102. },
  103. save: function(options) {
  104. if (!this.persist) {
  105. return;
  106. }
  107. var self = this;
  108. $(document).trigger('save.' + self.identifier, [self, options]);
  109. var options = $.extend({
  110. type: 'POST',
  111. data: self.getData(),
  112. success: function(data) {
  113. switch(data.status) {
  114. case 0:
  115. $(document).trigger('saved.' + self.identifier, [self, options, data]);
  116. break;
  117. default:
  118. case 1:
  119. $(document).trigger('save_fail.' + self.identifier, [self, options, data]);
  120. break;
  121. }
  122. }
  123. }, options);
  124. self.request(self.saveUrl(), options);
  125. },
  126. clone: function(options) {
  127. if (!this.persist) {
  128. return;
  129. }
  130. var self = this;
  131. $(document).trigger('clone.' + self.identifier, [self, options]);
  132. var options = $.extend({
  133. type: 'POST',
  134. success: function(data) {
  135. switch(data.status) {
  136. case 0:
  137. $(document).trigger('cloned.' + self.identifier, [self, options, data]);
  138. break;
  139. default:
  140. case 1:
  141. $(document).trigger('clone_fail.' + self.identifier, [self, options, data]);
  142. break;
  143. }
  144. }
  145. }, options);
  146. self.request(self.cloneUrl(), options);
  147. },
  148. delete: function(options) {
  149. if (!this.persist) {
  150. return;
  151. }
  152. var self = this;
  153. $(document).trigger('delete.' + self.identifier, [self, options]);
  154. var options = $.extend({
  155. type: 'POST',
  156. success: function(data) {
  157. switch(data.status) {
  158. case 0:
  159. $(document).trigger('deleted.' + self.identifier, [self, options, data]);
  160. break;
  161. default:
  162. case 1:
  163. $(document).trigger('delete_fail.' + self.identifier, [self, options, data]);
  164. break;
  165. }
  166. }
  167. }, options);
  168. self.request(self.deleteUrl(), options);
  169. },
  170. getData: function() {
  171. var self = this;
  172. var model = ko.sqoop.fixModel(self);
  173. var data = {};
  174. data[self.identifier] = ko.utils.stringifyJson(model);
  175. return data;
  176. },
  177. loadUrl: function(persist) {
  178. var self = this;
  179. return self.base_url + ((persist) ? self.id() : '');
  180. },
  181. saveUrl: function() {
  182. var self = this;
  183. if (self.id() > -1) {
  184. return self.base_url + self.id();
  185. } else {
  186. return self.base_url;
  187. }
  188. },
  189. cloneUrl: function() {
  190. var self = this;
  191. return self.base_url + self.id() + '/clone';
  192. },
  193. deleteUrl: function() {
  194. var self = this;
  195. return self.base_url + self.id() + '/delete';
  196. }
  197. });
  198. return {
  199. 'KOClass': KOClass,
  200. 'MinimalNode': MinimalNode,
  201. 'Node': Node,
  202. 'Model': Model
  203. }
  204. })($, undefined);