Przeglądaj źródła

Introduced the Model class with the basics for attributes creation.

fredck 11 lat temu
rodzic
commit
c3c1c32c62

+ 82 - 0
packages/ckeditor5-utils/src/mvc/model.js

@@ -0,0 +1,82 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+/**
+ * The base MVC model class.
+ *
+ * @class Model
+ */
+
+CKEDITOR.define( [ 'utils' ], function( utils ) {
+	/**
+	 * Creates a new Model instance.
+	 *
+	 * @param {Object} [attributes] The model state attributes to be set during the instance creation.
+	 * @param {Object} [properties] The properties to be appended to the instance during creation.
+	 * @method constructor
+	 */
+	function Model( attributes, properties ) {
+		/**
+		 * The internal hash containing the model's state.
+		 *
+		 * @property _attributes
+		 * @private
+		 */
+		Object.defineProperty( this, '_attributes', {
+			value: {}
+		} );
+
+		// Extend this instance with the additional (out of state) properties.
+		if ( properties ) {
+			utils.extend( this, properties );
+		}
+
+		// Initialize the attributes.
+		if ( attributes ) {
+			this.set( attributes );
+		}
+	}
+
+	utils.extend( Model.prototype, {
+		/**
+		 * Creates and sets the value of a model property of this object. This property will be part of the model state
+		 * and are observable.
+		 *
+		 * It accepts also a single object literal containing key/value pairs with properties to be set.
+		 *
+		 * @param {String} name The property name.
+		 * @param {*} value The property value.
+		 */
+		set: function( name, value ) {
+			// If the first parameter is an Object, we gonna interact through its properties.
+			if ( utils.isObject( name ) ) {
+				Object.keys( name ).forEach( function( attr ) {
+					this.set( attr, name[ attr ] );
+				}, this );
+
+				return;
+			}
+
+			Object.defineProperty( this, name, {
+				enumerable: true,
+				configurable: true,
+
+				get: function() {
+					return this._attributes[ name ];
+				},
+
+				set: function( value ) {
+					this._attributes[ name ] = value;
+				}
+			} );
+
+			this._attributes[ name ] = value;
+		}
+	} );
+
+	return Model;
+} );

+ 75 - 0
packages/ckeditor5-utils/tests/mvc/model/model.js

@@ -0,0 +1,75 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* globals describe, it, expect, beforeEach, bender */
+
+'use strict';
+
+var modules = bender.amd.require( 'mvc/model' );
+
+var car;
+
+describe( 'Model', function() {
+	beforeEach( 'Create a test model instance', function() {
+		var Model = modules[ 'mvc/model' ];
+
+		car = new Model( {
+			color: 'red',
+			year: 2015
+		} );
+	} );
+
+	//////////
+
+	it( 'should set _attributes on creation', function() {
+		expect( car._attributes ).to.deep.equal( {
+			color: 'red',
+			year: 2015
+		} );
+	} );
+
+	it( 'should get correctly after set', function() {
+		car.color = 'blue';
+
+		expect( car.color ).to.equal( 'blue' );
+		expect( car._attributes.color ).to.equal( 'blue' );
+	} );
+
+	it( 'should get correctly after setting _attributes', function() {
+		car._attributes.color = 'blue';
+
+		expect( car.color ).to.equal( 'blue' );
+	} );
+
+	//////////
+
+	describe( 'set()', function() {
+		it( 'should work when passing an object', function() {
+			car.set( {
+				color: 'blue',	// Override
+				wheels: 4,
+				seats: 5
+			} );
+
+			expect( car._attributes ).to.deep.equal( {
+				color: 'blue',
+				year: 2015,
+				wheels: 4,
+				seats: 5
+			} );
+		} );
+
+		it( 'should work when passing key/value', function() {
+			car.set( 'color', 'blue' );
+			car.set( 'wheels', 4 );
+
+			expect( car._attributes ).to.deep.equal( {
+				color: 'blue',
+				year: 2015,
+				wheels: 4
+			} );
+		} );
+	} );
+} );