8
0

model.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. /**
  7. * The base MVC model class.
  8. *
  9. * @class Model
  10. * @mixins EventEmitter
  11. */
  12. CKEDITOR.define( [ 'emittermixin', 'utils' ], function( EmitterMixin, utils ) {
  13. class Model {
  14. /**
  15. * Creates a new Model instance.
  16. *
  17. * @param {Object} [attributes] The model state attributes to be set during the instance creation.
  18. * @param {Object} [properties] The properties to be appended to the instance during creation.
  19. * @method constructor
  20. */
  21. constructor( attributes, properties ) {
  22. /**
  23. * The internal hash containing the model's state.
  24. *
  25. * @property _attributes
  26. * @private
  27. */
  28. this._attributes = {};
  29. // Extend this instance with the additional (out of state) properties.
  30. if ( properties ) {
  31. utils.extend( this, properties );
  32. }
  33. // Initialize the attributes.
  34. if ( attributes ) {
  35. this.set( attributes );
  36. }
  37. }
  38. /**
  39. * Creates and sets the value of a model attribute of this object. This attribute will be part of the model
  40. * state and will be observable.
  41. *
  42. * It accepts also a single object literal containing key/value pairs with attributes to be set.
  43. *
  44. * @param {String} name The attributes name.
  45. * @param {*} value The attributes value.
  46. */
  47. set( name, value ) {
  48. // If the first parameter is an Object, we gonna interact through its properties.
  49. if ( utils.isObject( name ) ) {
  50. Object.keys( name ).forEach( function( attr ) {
  51. this.set( attr, name[ attr ] );
  52. }, this );
  53. return;
  54. }
  55. Object.defineProperty( this, name, {
  56. enumerable: true,
  57. configurable: true,
  58. get: function() {
  59. return this._attributes[ name ];
  60. },
  61. set: function( value ) {
  62. var oldValue = this._attributes[ name ];
  63. if ( oldValue !== value ) {
  64. this._attributes[ name ] = value;
  65. this.fire( 'change', name, value, oldValue );
  66. this.fire( 'change:' + name, value, oldValue );
  67. }
  68. }
  69. } );
  70. this[ name ] = value;
  71. }
  72. }
  73. utils.extend( Model.prototype, EmitterMixin );
  74. return Model;
  75. } );
  76. /**
  77. * Fired when an attribute changed value.
  78. *
  79. * @event change
  80. * @param {String} name The attribute name.
  81. * @param {*} value The new attribute value.
  82. * @param {*} oldValue The previous attribute value.
  83. */
  84. /**
  85. * Fired when an specific attribute changed value.
  86. *
  87. * @event change:{attribute}
  88. * @param {*} value The new attribute value.
  89. * @param {*} oldValue The previous attribute value.
  90. */