BaseModel.es6.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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(
  7. /** @lends Drupal.quickedit.BaseModel# */ {
  8. /**
  9. * @constructs
  10. *
  11. * @augments Backbone.Model
  12. *
  13. * @param {object} options
  14. * Options for the base model-
  15. *
  16. * @return {Drupal.quickedit.BaseModel}
  17. * A quickedit base model.
  18. */
  19. initialize(options) {
  20. this.__initialized = true;
  21. return Backbone.Model.prototype.initialize.call(this, options);
  22. },
  23. /**
  24. * Set a value on the model
  25. *
  26. * @param {object|string} key
  27. * The key to set a value for.
  28. * @param {*} val
  29. * The value to set.
  30. * @param {object} [options]
  31. * Options for the model.
  32. *
  33. * @return {*}
  34. * The result of `Backbone.Model.prototype.set` with the specified
  35. * parameters.
  36. */
  37. set(key, val, options) {
  38. if (this.__initialized) {
  39. // Deal with both the "key", value and {key:value}-style arguments.
  40. if (typeof key === 'object') {
  41. key.validate = true;
  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. );
  53. })(Drupal, Backbone);