model.js 2.6 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. * @extends BasicClass
  11. */
  12. CKEDITOR.define( [ 'basicclass', 'utils' ], function( BasicClass, utils ) {
  13. var Model = BasicClass.extend( {
  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: function Model( attributes, properties ) {
  22. /**
  23. * The internal hash containing the model's state.
  24. *
  25. * @property _attributes
  26. * @private
  27. */
  28. Object.defineProperty( this, '_attributes', {
  29. value: {}
  30. } );
  31. // Extend this instance with the additional (out of state) properties.
  32. if ( properties ) {
  33. utils.extend( this, properties );
  34. }
  35. // Initialize the attributes.
  36. if ( attributes ) {
  37. this.set( attributes );
  38. }
  39. },
  40. /**
  41. * Creates and sets the value of a model attribute of this object. This attribute will be part of the model
  42. * state and will be observable.
  43. *
  44. * It accepts also a single object literal containing key/value pairs with attributes to be set.
  45. *
  46. * @param {String} name The attributes name.
  47. * @param {*} value The attributes value.
  48. */
  49. set: function( name, value ) {
  50. // If the first parameter is an Object, we gonna interact through its properties.
  51. if ( utils.isObject( name ) ) {
  52. Object.keys( name ).forEach( function( attr ) {
  53. this.set( attr, name[ attr ] );
  54. }, this );
  55. return;
  56. }
  57. Object.defineProperty( this, name, {
  58. enumerable: true,
  59. configurable: true,
  60. get: function() {
  61. return this._attributes[ name ];
  62. },
  63. set: function( value ) {
  64. var oldValue = this._attributes[ name ];
  65. if ( oldValue !== value ) {
  66. this._attributes[ name ] = value;
  67. this.fire( 'change', name, value, oldValue );
  68. this.fire( 'change:' + name, value, oldValue );
  69. }
  70. }
  71. } );
  72. this[ name ] = value;
  73. }
  74. } );
  75. return Model;
  76. } );
  77. /**
  78. * Fired when an attribute changed value.
  79. *
  80. * @event change
  81. * @param {String} name The attribute name.
  82. * @param {*} value The new attribute value.
  83. * @param {*} oldValue The previous attribute value.
  84. */
  85. /**
  86. * Fired when an specific attribute changed value.
  87. *
  88. * @event change:{attribute}
  89. * @param {*} value The new attribute value.
  90. * @param {*} oldValue The previous attribute value.
  91. */