Răsfoiți Sursa

Simplified the initialization of ui.Controller#_anonymousCollection.

Aleksander Nowodzinski 9 ani în urmă
părinte
comite
6bca0935aa

+ 15 - 25
packages/ckeditor5-ui/src/controller.js

@@ -9,7 +9,7 @@ import CKEditorError from '../utils/ckeditorerror.js';
 import EmitterMixin from '../utils/emittermixin.js';
 import mix from '../utils/mix.js';
 
-const anon = '_anonymous';
+const anon = '$anonymous';
 
 /**
  * Basic Controller class.
@@ -56,14 +56,21 @@ export default class Controller {
 			idProperty: 'name'
 		} );
 
+		/**
+		 * Anonymous collection of this controller instance. It groups child controllers
+		 * which are not to be handled by `Controller#collections`–to–`View#region`
+		 * automation. It also means their views must be handled individually
+		 * by the view, i.e. passed as members of {@link ui.TemplateDefinition#children}.
+		 *
+		 * @protected
+		 * @member {ui.ControllerCollection} ui.Controller#_anonymousCollection
+		 */
+		this.collections.add( this._anonymousCollection = new ControllerCollection( anon ) );
+
 		// Listen to {@link ControllerCollection#add} and {@link ControllerCollection#remove}
 		// of newly added Collection to synchronize this controller's view and children
 		// controllers' views in the future.
 		this.collections.on( 'add', ( evt, collection ) => {
-			if ( isAnonymous( collection ) ) {
-				return;
-			}
-
 			// Set the {@link ControllerCollection#parent} to this controller.
 			// It allows the collection to determine the {@link #ready} state of this controller
 			// and accordingly initialize a child controller when added.
@@ -249,6 +256,9 @@ export default class Controller {
 
 		for ( collection of this.collections ) {
 			for ( childController of collection ) {
+				// Anonymous collection {@link ui.Controller#_anonymousCollection} does not allow
+				// automated controller-to-view binding, because there's no such thing as
+				// anonymous Region in the View instance.
 				if ( !isAnonymous( collection ) && this.view && childController.view ) {
 					this.view.regions.get( collection.name ).views.add( childController.view );
 				}
@@ -259,26 +269,6 @@ export default class Controller {
 
 		return Promise.all( promises );
 	}
-
-	/**
-	 * Anonymous collection of this controller instance. It groups child controllers
-	 * which are not to be handled by `Controller#collections`–to–`View#region`
-	 * automation. It also means their views must be handled individually
-	 * by the view, i.e. passed as members of {@link ui.TemplateDefinition#children}.
-	 *
-	 * @protected
-	 * @type {ui.ControllerCollection}
-	 */
-	get _anonymousCollection() {
-		let collection = this.collections.get( anon );
-
-		if ( !collection ) {
-			collection = new ControllerCollection( anon );
-			this.collections.add( collection );
-		}
-
-		return collection;
-	}
 }
 
 mix( Controller, EmitterMixin );

+ 33 - 41
packages/ckeditor5-ui/tests/controller.js

@@ -26,7 +26,9 @@ describe( 'Controller', () => {
 			expect( controller.model ).to.be.null;
 			expect( controller.ready ).to.be.false;
 			expect( controller.view ).to.be.null;
-			expect( controller.collections.length ).to.be.equal( 0 );
+
+			expect( controller.collections.length ).to.equal( 1 );
+			expect( controller.collections.get( '$anonymous' ) ).to.be.instanceOf( ControllerCollection );
 		} );
 
 		it( 'should accept model and view', () => {
@@ -34,8 +36,8 @@ describe( 'Controller', () => {
 			const view = new View();
 			const controller = new Controller( model, view );
 
-			expect( controller.model ).to.be.equal( model );
-			expect( controller.view ).to.be.equal( view );
+			expect( controller.model ).to.equal( model );
+			expect( controller.view ).to.equal( view );
 		} );
 	} );
 
@@ -92,8 +94,8 @@ describe( 'Controller', () => {
 			buttonCollection.add( childController2 );
 
 			return parentController.init().then( () => {
-				expect( buttonCollection.get( 0 ) ).to.be.equal( childController1 );
-				expect( buttonCollection.get( 1 ) ).to.be.equal( childController2 );
+				expect( buttonCollection.get( 0 ) ).to.equal( childController1 );
+				expect( buttonCollection.get( 1 ) ).to.equal( childController2 );
 
 				sinon.assert.calledOnce( spy1 );
 				sinon.assert.calledOnce( spy2 );
@@ -108,23 +110,18 @@ describe( 'Controller', () => {
 			const spy1 = testUtils.sinon.spy( child1, 'init' );
 			const spy2 = testUtils.sinon.spy( child2, 'init' );
 
-			let collection = parentController.collections.get( '_anonymous' );
-
-			expect( collection ).to.be.null;
+			const collection = parentController.collections.get( '$anonymous' );
 
 			parentController.add( child1 );
 			parentController.add( child2 );
 
-			collection = parentController.collections.get( '_anonymous' );
-
-			expect( collection ).to.be.instanceOf( ControllerCollection );
 			expect( collection ).to.have.length( 2 );
-			expect( collection.get( 0 ) ).to.be.equal( child1 );
-			expect( collection.get( 1 ) ).to.be.equal( child2 );
+			expect( collection.get( 0 ) ).to.equal( child1 );
+			expect( collection.get( 1 ) ).to.equal( child2 );
 
 			return parentController.init().then( () => {
-				expect( collection.get( 0 ) ).to.be.equal( child1 );
-				expect( collection.get( 1 ) ).to.be.equal( child2 );
+				expect( collection.get( 0 ) ).to.equal( child1 );
+				expect( collection.get( 1 ) ).to.equal( child2 );
 
 				sinon.assert.calledOnce( spy1 );
 				sinon.assert.calledOnce( spy2 );
@@ -151,8 +148,8 @@ describe( 'Controller', () => {
 			parentController.add( 'x', child2 );
 
 			expect( collection ).to.have.length( 2 );
-			expect( collection.get( 0 ) ).to.be.equal( child1 );
-			expect( collection.get( 1 ) ).to.be.equal( child2 );
+			expect( collection.get( 0 ) ).to.equal( child1 );
+			expect( collection.get( 1 ) ).to.equal( child2 );
 		} );
 
 		it( 'should add a controller at specific index', () => {
@@ -165,8 +162,8 @@ describe( 'Controller', () => {
 			parentController.add( 'x', child2, 0 );
 
 			expect( collection ).to.have.length( 2 );
-			expect( collection.get( 0 ) ).to.be.equal( child2 );
-			expect( collection.get( 1 ) ).to.be.equal( child1 );
+			expect( collection.get( 0 ) ).to.equal( child2 );
+			expect( collection.get( 1 ) ).to.equal( child1 );
 		} );
 
 		it( 'should add a controller to the anonymous collection', () => {
@@ -174,19 +171,14 @@ describe( 'Controller', () => {
 			const child1 = new Controller( null, new View() );
 			const child2 = new Controller( null, new View() );
 
-			let collection = parentController.collections.get( '_anonymous' );
-
-			expect( collection ).to.be.null;
+			const collection = parentController.collections.get( '$anonymous' );
 
 			parentController.add( child1 );
 			parentController.add( child2 );
 
-			collection = parentController.collections.get( '_anonymous' );
-
-			expect( collection ).to.be.instanceOf( ControllerCollection );
 			expect( collection ).to.have.length( 2 );
-			expect( collection.get( 0 ) ).to.be.equal( child1 );
-			expect( collection.get( 1 ) ).to.be.equal( child2 );
+			expect( collection.get( 0 ) ).to.equal( child1 );
+			expect( collection.get( 1 ) ).to.equal( child2 );
 		} );
 	} );
 
@@ -207,9 +199,9 @@ describe( 'Controller', () => {
 			const removed = parentController.remove( 'x', child2 );
 
 			expect( collection ).to.have.length( 2 );
-			expect( collection.get( 0 ) ).to.be.equal( child1 );
-			expect( collection.get( 1 ) ).to.be.equal( child3 );
-			expect( removed ).to.be.equal( child2 );
+			expect( collection.get( 0 ) ).to.equal( child1 );
+			expect( collection.get( 1 ) ).to.equal( child3 );
+			expect( removed ).to.equal( child2 );
 		} );
 
 		it( 'should remove a controller from specific collection – by index', () => {
@@ -226,9 +218,9 @@ describe( 'Controller', () => {
 			const removed = parentController.remove( 'x', 1 );
 
 			expect( collection ).to.have.length( 2 );
-			expect( collection.get( 0 ) ).to.be.equal( child1 );
-			expect( collection.get( 1 ) ).to.be.equal( child3 );
-			expect( removed ).to.be.equal( child2 );
+			expect( collection.get( 0 ) ).to.equal( child1 );
+			expect( collection.get( 1 ) ).to.equal( child3 );
+			expect( removed ).to.equal( child2 );
 		} );
 
 		it( 'should remove a controller from the anonymous collection', () => {
@@ -241,13 +233,13 @@ describe( 'Controller', () => {
 			parentController.add( child2 );
 			parentController.add( child3 );
 
-			const collection = parentController.collections.get( '_anonymous' );
+			const collection = parentController.collections.get( '$anonymous' );
 			const removed = parentController.remove( child2 );
 
 			expect( collection ).to.have.length( 2 );
-			expect( collection.get( 0 ) ).to.be.equal( child1 );
-			expect( collection.get( 1 ) ).to.be.equal( child3 );
-			expect( removed ).to.be.equal( child2 );
+			expect( collection.get( 0 ) ).to.equal( child1 );
+			expect( collection.get( 1 ) ).to.equal( child3 );
+			expect( removed ).to.equal( child2 );
 		} );
 	} );
 
@@ -266,7 +258,7 @@ describe( 'Controller', () => {
 						return collection.add( childController );
 					} )
 					.then( () => {
-						expect( collection.get( 0 ) ).to.be.equal( childController );
+						expect( collection.get( 0 ) ).to.equal( childController );
 					} );
 			} );
 
@@ -314,8 +306,8 @@ describe( 'Controller', () => {
 					.then( () => {
 						const region = parentController.view.regions.get( 'x' );
 
-						expect( region.views.get( 0 ) ).to.be.equal( childView2 );
-						expect( region.views.get( 1 ) ).to.be.equal( childView1 );
+						expect( region.views.get( 0 ) ).to.equal( childView2 );
+						expect( region.views.get( 1 ) ).to.equal( childView1 );
 					} );
 			} );
 
@@ -531,7 +523,7 @@ describe( 'Controller', () => {
 
 			controller.addCollection( 'foo' );
 
-			expect( controller.collections ).to.have.length( 1 );
+			expect( controller.collections ).to.have.length( 2 );
 			expect( controller.collections.get( 'foo' ).name ).to.equal( 'foo' );
 		} );