koify.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 Node = KOClass(function(options) {
  42. var self = this;
  43. var options = options || {};
  44. self.options = options;
  45. self.initialize(options);
  46. }, {
  47. persist: true,
  48. identifier: undefined,
  49. modelClass: undefined,
  50. initialize: function(options) {
  51. var self = this;
  52. if (options.modelDict) {
  53. self.model = new self.modelClass(options.modelDict);
  54. } else {
  55. self.model = $.extend(true, options.model, {});
  56. }
  57. if ('__ko_mapping__' in self) {
  58. ko.mapping.fromJS(self.model, {
  59. 'ignore': ['parent', 'initialize']
  60. }, self);
  61. } else {
  62. var mapped = ko.mapping.fromJS(self.model, {
  63. 'ignore': ['parent', 'initialize']
  64. });
  65. $.extend(self, mapped);
  66. }
  67. },
  68. request: function(url, options) {
  69. var self = this;
  70. var request = $.extend({
  71. url: url,
  72. dataType: 'json',
  73. type: 'GET',
  74. success: $.noop,
  75. error: $.noop
  76. }, options || {});
  77. $.ajax(request);
  78. },
  79. load: function(options) {
  80. var self = this;
  81. $(document).trigger('load.' + self.identifier, [self, options]);
  82. var options = $.extend({
  83. success: function(data) {
  84. switch(data.status) {
  85. case 0:
  86. self.initialize({modelDict: data[self.identifier]});
  87. $(document).trigger('loaded.' + self.identifier, [self, options]);
  88. break;
  89. default:
  90. case 1:
  91. $(document).trigger('load_fail.' + self.identifier, [self, options, data]);
  92. break;
  93. }
  94. }
  95. }, options);
  96. self.request(self.loadUrl(), options);
  97. },
  98. save: function(options) {
  99. if (!this.persist) {
  100. return;
  101. }
  102. var self = this;
  103. $(document).trigger('save.' + self.identifier, [self, options]);
  104. var options = $.extend({
  105. type: 'POST',
  106. data: self.getData(),
  107. success: function(data) {
  108. switch(data.status) {
  109. case 0:
  110. $(document).trigger('saved.' + self.identifier, [self, options, data]);
  111. break;
  112. default:
  113. case 1:
  114. $(document).trigger('save_fail.' + self.identifier, [self, options, data]);
  115. break;
  116. }
  117. }
  118. }, options);
  119. self.request(self.saveUrl(), options);
  120. },
  121. clone: function(options) {
  122. if (!this.persist) {
  123. return;
  124. }
  125. var self = this;
  126. $(document).trigger('clone.' + self.identifier, [self, options]);
  127. var options = $.extend({
  128. type: 'POST',
  129. success: function(data) {
  130. switch(data.status) {
  131. case 0:
  132. $(document).trigger('cloned.' + self.identifier, [self, options, data]);
  133. break;
  134. default:
  135. case 1:
  136. $(document).trigger('clone_fail.' + self.identifier, [self, options, data]);
  137. break;
  138. }
  139. }
  140. }, options);
  141. self.request(self.cloneUrl(), options);
  142. },
  143. delete: function(options) {
  144. if (!this.persist) {
  145. return;
  146. }
  147. var self = this;
  148. $(document).trigger('delete.' + self.identifier, [self, options]);
  149. var options = $.extend({
  150. type: 'POST',
  151. success: function(data) {
  152. switch(data.status) {
  153. case 0:
  154. $(document).trigger('deleted.' + self.identifier, [self, options, data]);
  155. break;
  156. default:
  157. case 1:
  158. $(document).trigger('delete_fail.' + self.identifier, [self, options, data]);
  159. break;
  160. }
  161. }
  162. }, options);
  163. self.request(self.deleteUrl(), options);
  164. },
  165. getData: function() {
  166. var self = this;
  167. data = {};
  168. data[self.identifier] = ko.mapping.toJSON(self);
  169. return data;
  170. },
  171. loadUrl: function(persist) {
  172. var self = this;
  173. return self.base_url + ((persist) ? self.id() : '');
  174. },
  175. saveUrl: function() {
  176. var self = this;
  177. if (self.id() > -1) {
  178. return self.base_url + self.id();
  179. } else {
  180. return self.base_url;
  181. }
  182. },
  183. cloneUrl: function() {
  184. var self = this;
  185. return self.base_url + self.id() + '/clone';
  186. },
  187. deleteUrl: function() {
  188. var self = this;
  189. return self.base_url + self.id() + '/delete';
  190. }
  191. });
  192. return {
  193. KOClass: KOClass,
  194. Node: Node,
  195. Model: Model
  196. }
  197. })($, undefined);