BaseModel.es6.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * @file
  3. * A Backbone Model subclass that enforces validation when calling set().
  4. */
  5. (function (Drupal, Backbone) {
  6. Drupal.quickedit.BaseModel = Backbone.Model.extend(/** @lends Drupal.quickedit.BaseModel# */{
  7. /**
  8. * @constructs
  9. *
  10. * @augments Backbone.Model
  11. *
  12. * @param {object} options
  13. * Options for the base model-
  14. *
  15. * @return {Drupal.quickedit.BaseModel}
  16. * A quickedit base model.
  17. */
  18. initialize(options) {
  19. this.__initialized = true;
  20. return Backbone.Model.prototype.initialize.call(this, options);
  21. },
  22. /**
  23. * Set a value on the model
  24. *
  25. * @param {object|string} key
  26. * The key to set a value for.
  27. * @param {*} val
  28. * The value to set.
  29. * @param {object} [options]
  30. * Options for the model.
  31. *
  32. * @return {*}
  33. * The result of `Backbone.Model.prototype.set` with the specified
  34. * parameters.
  35. */
  36. set(key, val, options) {
  37. if (this.__initialized) {
  38. // Deal with both the "key", value and {key:value}-style arguments.
  39. if (typeof key === 'object') {
  40. key.validate = true;
  41. }
  42. else {
  43. if (!options) {
  44. options = {};
  45. }
  46. options.validate = true;
  47. }
  48. }
  49. return Backbone.Model.prototype.set.call(this, key, val, options);
  50. },
  51. });
  52. }(Drupal, Backbone));