瀏覽代碼

Merged a number of Model#bind errors to reduce the amount of code and documentation.

Aleksander Nowodzinski 10 年之前
父節點
當前提交
8b43b4822a
共有 2 個文件被更改,包括 23 次插入68 次删除
  1. 12 42
      packages/ckeditor5-ui/src/model.js
  2. 11 26
      packages/ckeditor5-ui/tests/mvc/model/model.js

+ 12 - 42
packages/ckeditor5-ui/src/model.js

@@ -143,21 +143,16 @@ CKEDITOR.define( [ 'emittermixin', 'ckeditorerror', 'utils' ], ( EmitterMixin, C
 		bind() {
 			const bindAttrs = [].slice.call( arguments );
 
-			if ( !bindAttrs.length ) {
+			if ( !bindAttrs.length || !isStringArray( bindAttrs ) ) {
 				/**
-				 * No attributes supplied.
-				 *
-				 * @error model-bind-no-attrs
-				 */
-				throw new CKEditorError( 'model-bind-no-attrs: No attributes supplied.' );
-			} else if ( !isStringArray( bindAttrs ) ) {
-				/**
-				 * Attributes must be strings.
+				 * All attributes must be strings.
 				 *
 				 * @error model-bind-wrong-attrs
 				 */
-				throw new CKEditorError( 'model-bind-wrong-attrs: Attributes must be strings.' );
-			} else if ( ( new Set( bindAttrs ) ).size !== bindAttrs.length ) {
+				throw new CKEditorError( 'model-bind-wrong-attrs: All attributes must be strings.' );
+			}
+
+			if ( ( new Set( bindAttrs ) ).size !== bindAttrs.length ) {
 				/**
 				 * Attributes must be unique.
 				 *
@@ -213,20 +208,11 @@ CKEDITOR.define( [ 'emittermixin', 'ckeditorerror', 'utils' ], ( EmitterMixin, C
 			const toModel = arguments[ 0 ];
 			let toAttrs = [].slice.call( arguments, 1 );
 
-			if ( !toModel ) {
-				/**
-				 * No model supplied.
-				 *
-				 * @error model-bind-to-no-model
-				 */
-				throw new CKEditorError( 'model-bind-to-no-model: No model supplied.' );
-			}
-
-			if ( !( toModel instanceof Model ) ) {
+			if ( !toModel || !( toModel instanceof Model ) ) {
 				/**
 				 * An instance of Model is required.
 				 *
-				 * @error model-bind-to-wrong-model:
+				 * @error model-bind-to-wrong-model
 				 */
 				throw new CKEditorError( 'model-bind-to-wrong-model: An instance of Model is required.' );
 			}
@@ -252,23 +238,14 @@ CKEDITOR.define( [ 'emittermixin', 'ckeditorerror', 'utils' ], ( EmitterMixin, C
 			}
 
 			// Eliminate A.bind( 'x' ).to( B, 'y' ), when B.y == undefined.
-			if ( !hasAttributes( toModel, toAttrs ) ) {
-				/**
-				 * Model has no such attribute(s).
-				 *
-				 * @error model-bind-to-missing-model-attr
-				 */
-				throw new CKEditorError( 'model-bind-to-missing-model-attr: Model has no such attribute(s).' );
-			}
-
 			// Eliminate A.bind( 'x' ).to( B ), when B.x == undefined.
-			if ( !toAttrs.length && !hasAttributes( toModel, this._bindAttrs ) ) {
+			if ( !hasAttributes( toModel, toAttrs ) || ( !toAttrs.length && !hasAttributes( toModel, this._bindAttrs ) ) ) {
 				/**
 				 * Model has no such attribute(s).
 				 *
-				 * @error model-bind-to-missing-to-attr
+				 * @error model-bind-to-missing-attr
 				 */
-				throw new CKEditorError( 'model-bind-to-missing-to-attr: Model has no such attribute(s).' );
+				throw new CKEditorError( 'model-bind-to-missing-attr: Model has no such attribute(s).' );
 			}
 
 			// Eliminate A.bind( 'x', 'y' ).to( B ).to( C ) when no trailing .as().
@@ -306,14 +283,7 @@ CKEDITOR.define( [ 'emittermixin', 'ckeditorerror', 'utils' ], ( EmitterMixin, C
 		 * @param {Function} callback A callback to combine model's attributes.
 		 */
 		_bindAs( callback ) {
-			if ( !callback ) {
-				/**
-				 * No callback function supplied.
-				 *
-				 * @error model-bind-as-no-callback
-				 */
-				throw new CKEditorError( 'model-bind-as-no-callback: No callback function supplied.' );
-			} else if ( typeof callback !== 'function' ) {
+			if ( !callback || typeof callback !== 'function' ) {
 				/**
 				 * Callback must be a Function.
 				 *

+ 11 - 26
packages/ckeditor5-ui/tests/mvc/model/model.js

@@ -201,15 +201,11 @@ describe( 'Model', () => {
 			expect( car.bind( 'nonexistent' ) ).to.contain.keys( 'to' );
 		} );
 
-		it( 'should throw when no attributes specified', () => {
-			const CKEditorError = modules.ckeditorerror;
-
+		it( 'should throw when attributes are not strings', () => {
 			expect( () => {
 				car.bind();
-			} ).to.throw( CKEditorError, /model-bind-no-attrs/ );
-		} );
+			} ).to.throw( CKEditorError, /model-bind-wrong-attrs/ );
 
-		it( 'should throw when attributes are not strings', () => {
 			expect( () => {
 				car.bind( new Date() );
 			} ).to.throw( CKEditorError, /model-bind-wrong-attrs/ );
@@ -250,15 +246,13 @@ describe( 'Model', () => {
 				expect( returned ).to.have.property( 'as' );
 			} );
 
-			it( 'should throw when no .to() model', () => {
+			it( 'should throw when .to() model is not Model', () => {
 				expect( () => {
 					car.bind( 'color' ).to();
-				} ).to.throw( CKEditorError, /model-bind-to-no-model/ );
-			} );
+				} ).to.throw( CKEditorError, /model-bind-to-wrong-model/ );
 
-			it( 'should throw when .to() model is not Model', () => {
 				expect( () => {
-					car.bind( 'color' ).to( 'it\'s not a model' );
+					car.bind( 'year' ).to( 'it\'s not a model' );
 				} ).to.throw( CKEditorError, /model-bind-to-wrong-model/ );
 			} );
 
@@ -294,20 +288,16 @@ describe( 'Model', () => {
 				} ).to.throw( CKEditorError, /model-bind-to-attrs-length/ );
 			} );
 
-			it( 'should throw when binding to an nonexistent attribute in the model', () => {
+			it( 'should throw when no attribute specified and those from bind() don\'t exist in to() model', () => {
 				const vehicle = new Car();
 
 				expect( () => {
 					vehicle.bind( 'color' ).to( car, 'nonexistent in car' );
-				} ).to.throw( CKEditorError, /model-bind-to-missing-model-attr/ );
-			} );
-
-			it( 'should throw when no attribute specified and those from bind() don\'t exist in to() model', () => {
-				const vehicle = new Car();
+				} ).to.throw( CKEditorError, /model-bind-to-missing-attr/ );
 
 				expect( () => {
 					vehicle.bind( 'nonexistent in car' ).to( car );
-				} ).to.throw( CKEditorError, /model-bind-to-missing-to-attr/ );
+				} ).to.throw( CKEditorError, /model-bind-to-missing-attr/ );
 			} );
 
 			it( 'should throw when to() more than once and multiple attributes', () => {
@@ -432,18 +422,13 @@ describe( 'Model', () => {
 					).to.be.undefined;
 				} );
 
-				it( 'should throw when no function specified', () => {
+				it( 'should throw when not a function passed', () => {
 					const car1 = new Car( { color: 'brown' } );
 					const car2 = new Car( { color: 'green' } );
 
 					expect( () => {
-						car.bind( 'color' ).to( car1, 'color' ).to( car2, 'color' ).as();
-					} ).to.throw( CKEditorError, /model-bind-as-no-callback/ );
-				} );
-
-				it( 'should throw when not a function passed', () => {
-					const car1 = new Car( { color: 'brown' } );
-					const car2 = new Car( { color: 'green' } );
+						car.bind( 'year' ).to( car1, 'color' ).to( car2, 'color' ).as();
+					} ).to.throw( CKEditorError, /model-bind-as-wrong-callback/ );
 
 					expect( () => {
 						car.bind( 'color' ).to( car1, 'color' ).to( car2, 'color' ).as( 'not-a-function' );