PageRenderTime 1199ms CodeModel.GetById 41ms RepoModel.GetById 1ms app.codeStats 1ms

/auiplugin/src/main/resources/experimental/js/atlassian/restfultable/restfultable.entrymodel.js

https://bitbucket.org/mquail/aui-archive-pdl
JavaScript | 221 lines | 120 code | 42 blank | 59 comment | 31 complexity | ad29aed951eaed9d26d0d296e330488b MD5 | raw file
Possible License(s): Apache-2.0
  1. (function ($) {
  2. /**
  3. * A class provided to fill some gaps with the out of the box Backbone.Model class. Most notiably the inability
  4. * to send ONLY modified attributes back to the server.
  5. *
  6. * @class EntryModel
  7. * @namespace AJS.RestfulTable
  8. */
  9. AJS.RestfulTable.EntryModel = Backbone.Model.extend({
  10. /**
  11. * Overrides default save handler to only save (send to server) attributes that have changed.
  12. * Also provides some default error handling.
  13. *
  14. * @override
  15. * @param attributes
  16. * @param options
  17. */
  18. save: function (attributes, options) {
  19. options = options || {};
  20. var instance = this,
  21. Model,
  22. syncModel,
  23. error = options.error, // we override, so store original
  24. success = options.success;
  25. // override error handler to provide some defaults
  26. options.error = function (model, xhr) {
  27. var data = $.parseJSON(xhr.responseText || xhr.data);
  28. instance._serverErrorHandler(xhr);
  29. // call original error handler
  30. if (error) {
  31. error.call(instance, instance, data, xhr);
  32. }
  33. };
  34. // if it is a new model, we don't have to worry about updating only changed attributes because they are all new
  35. if (this.isNew()) {
  36. // call super
  37. Backbone.Model.prototype.save.call(this, attributes, options);
  38. // only go to server if something has changed
  39. } else if (attributes) {
  40. // create temporary model
  41. Model = Backbone.Model.extend({
  42. url: this.url()
  43. });
  44. syncModel = new Model({
  45. id: this.id
  46. });
  47. options.success = function (model, xhr) {
  48. // update original model with saved attributes
  49. instance.clear().set(model.toJSON());
  50. // call original success handler
  51. if (success) {
  52. success.call(instance, instance, xhr);
  53. }
  54. };
  55. // update temporary model with the changed attributes
  56. syncModel.save(attributes, options);
  57. }
  58. },
  59. /**
  60. * Destroys the model on the server. We need to override the default method as it does not support sending of
  61. * query paramaters.
  62. *
  63. * @override
  64. * @param options
  65. * ... {function} success - Server success callback
  66. * ... {function} error - Server error callback
  67. * ... {object} data
  68. *
  69. * @return AJS.RestfulTable.EntryModel
  70. */
  71. destroy: function (options) {
  72. options = options || {};
  73. var instance = this,
  74. url = this.url(),
  75. data;
  76. if (options.data) {
  77. data = $.param(options.data);
  78. }
  79. if (data !== "") {
  80. // we need to add to the url as the data param does not work for jQuery DELETE requests
  81. url = url + "?" + data;
  82. }
  83. $.ajax({
  84. url: url,
  85. type: "DELETE",
  86. dataType: "json",
  87. success: function (data) {
  88. if(instance.collection){
  89. instance.collection.remove(instance);
  90. }
  91. if (options.success) {
  92. options.success.call(instance, data);
  93. }
  94. },
  95. error: function (xhr) {
  96. instance._serverErrorHandler(xhr);
  97. if (options.error) {
  98. options.error.call(instance, xhr);
  99. }
  100. }
  101. });
  102. return this;
  103. },
  104. /**
  105. * A more complex lookup for changed attributes then default backbone one.
  106. *
  107. * @param attributes
  108. */
  109. changedAttributes: function (attributes) {
  110. var changed = {},
  111. current = this.toJSON();
  112. $.each(attributes, function (name, value) {
  113. if (!current[name]) {
  114. if (typeof value === "string") {
  115. if ($.trim(value) !== "") {
  116. changed[name] = value;
  117. }
  118. } else if ($.isArray(value)) {
  119. if (value.length !== 0) {
  120. changed[name] = value;
  121. }
  122. } else {
  123. changed[name] = value;
  124. }
  125. } else if (current[name] && current[name] !== value) {
  126. if (typeof value === "object") {
  127. if (!_.isEqual(value, current[name])) {
  128. changed[name] = value;
  129. }
  130. } else {
  131. changed[name] = value;
  132. }
  133. }
  134. });
  135. if (!_.isEmpty(changed)) {
  136. this.addExpand(changed);
  137. return changed;
  138. }
  139. },
  140. /**
  141. * Useful point to override if you always want to add an expand to your rest calls.
  142. *
  143. * @param changed attributes that have already changed
  144. */
  145. addExpand: function (changed){},
  146. /**
  147. * Throws a server error event unless user input validation error (status 400)
  148. *
  149. * @param xhr
  150. */
  151. _serverErrorHandler: function (xhr) {
  152. var data;
  153. if (xhr.status !== 400) {
  154. data = $.parseJSON(xhr.responseText || xhr.data);
  155. AJS.triggerEvtForInst(AJS.RestfulTable.Events.SERVER_ERROR, this, [data, xhr]);
  156. }
  157. },
  158. /**
  159. * Fetches values, with some generic error handling
  160. *
  161. * @override
  162. * @param options
  163. */
  164. fetch: function (options) {
  165. options = options || {};
  166. var instance = this,
  167. error = options.error;
  168. this.clear(); // clear the model, so we do not merge the old with the new
  169. options.error = function (model, xhr) {
  170. instance._serverErrorHandler(xhr);
  171. if (error) {
  172. error.apply(this, arguments);
  173. }
  174. };
  175. // call super
  176. Backbone.Model.prototype.fetch.call(this, options);
  177. }
  178. });
  179. })(AJS.$);