Browse Source

Fixed: Model#set throws when re–setting an attribute of undefined value.

Aleksander Nowodzinski 10 years ago
parent
commit
a7df650aef
2 changed files with 19 additions and 1 deletions
  1. 3 1
      packages/ckeditor5-ui/src/model.js
  2. 16 0
      packages/ckeditor5-ui/tests/model.js

+ 3 - 1
packages/ckeditor5-ui/src/model.js

@@ -177,7 +177,9 @@ export default class Model {
 			set: ( value ) => {
 				const oldValue = this._attributes[ name ];
 
-				if ( oldValue !== value ) {
+				// Allow undefined as an initial value like A.set( 'x', undefined ) (#132).
+				// Note: When _attributes has no such own property, then its value is undefined.
+				if ( oldValue !== value || !this._attributes.hasOwnProperty( name ) ) {
 					this._attributes[ name ] = value;
 					this.fire( 'change', name, value, oldValue );
 					this.fire( 'change:' + name, value, oldValue );

+ 16 - 0
packages/ckeditor5-ui/tests/model.js

@@ -164,6 +164,22 @@ describe( 'Model', () => {
 
 			expect( car.method ).to.be.a( 'function' );
 		} );
+
+		it( 'should allow setting attributes with undefined value', () => {
+			let spy = sinon.spy();
+
+			car.on( 'change', spy );
+			car.set( 'seats', undefined );
+
+			sinon.assert.calledOnce( spy );
+			expect( car._attributes ).to.contain.keys( 'seats' );
+			expect( car.seats ).to.be.undefined;
+
+			car.set( 'seats', 5 );
+
+			sinon.assert.calledTwice( spy );
+			expect( car._attributes ).to.have.property( 'seats', 5 );
+		} );
 	} );
 
 	describe( 'extend', () => {