PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/reviewboard/static/rb/js/collections/baseCollection.es6.js

http://github.com/reviewboard/reviewboard
JavaScript | 58 lines | 16 code | 3 blank | 39 comment | 1 complexity | 21dafe7e824137529b66623fc240f2dd MD5 | raw file
Possible License(s): GPL-2.0
  1. /**
  2. * The base class used for Review Board collections.
  3. *
  4. * This is a thin subclass over Backbone.Collection that just provides
  5. * some useful additional abilities.
  6. */
  7. RB.BaseCollection = Backbone.Collection.extend({
  8. /**
  9. * Fetch models from the server.
  10. *
  11. * This behaves just like Backbone.Collection.fetch, except it
  12. * takes a context parameter for callbacks.
  13. *
  14. * Args:
  15. * options (object):
  16. * Options for the fetch operation.
  17. *
  18. * context (object):
  19. * Context to be used when calling success/error/complete
  20. * callbacks.
  21. */
  22. fetch(options={}, context=undefined) {
  23. options = _.bindCallbacks(options, context);
  24. return Backbone.Collection.prototype.fetch.call(this, options);
  25. },
  26. /**
  27. * Handle all AJAX communication for the collection.
  28. *
  29. * Backbone.js will internally call the model's sync function to
  30. * communicate with the server, which usually uses Backbone.sync.
  31. *
  32. * This will parse error response from Review Board so we can provide
  33. * a more meaningful error callback.
  34. *
  35. * Args:
  36. * method (string):
  37. * The HTTP method to use for the AJAX request.
  38. *
  39. * model (object):
  40. * The model to sync.
  41. *
  42. * options (object):
  43. * Options for the sync operation.
  44. */
  45. sync(method, model, options={}) {
  46. return Backbone.sync.call(this, method, model, _.defaults({
  47. error: xhr => {
  48. RB.storeAPIError(xhr);
  49. if (_.isFunction(options.error)) {
  50. options.error(xhr);
  51. }
  52. }
  53. }, options));
  54. }
  55. });