Przeglądaj źródła

Simplified setting #ready state of the ViewCollection.

Aleksander Nowodziński 9 lat temu
rodzic
commit
4ad4b45176

+ 0 - 7
packages/ckeditor5-ui/src/view.js

@@ -65,16 +65,9 @@ export default class View {
 		// Let the new collection determine the {@link ui.View#ready} state of this view and,
 		// accordingly, initialize (or not) children views as they are added in the future.
 		this._viewCollections.on( 'add', ( evt, collection ) => {
-			collection.bind( 'ready' ).to( this );
 			collection.locale = locale;
 		} );
 
-		// Once the collection is removed from the view, the {@link ui.View#ready} binding
-		// should be removed.
-		this._viewCollections.on( 'remove', ( evt, collection ) => {
-			collection.unbind( 'ready' );
-		} );
-
 		/**
 		 * A collection of view instances, which have been added directly
 		 * into the {@link ui.View.template#children}.

+ 4 - 2
packages/ckeditor5-ui/src/viewcollection.js

@@ -85,7 +85,7 @@ export default class ViewCollection extends Collection {
 			 *
 			 * @error ui-viewcollection-init-reinit
 			 */
-			throw new CKEditorError( 'ui-viewviewcollection-init-reinit: This ViewCollection has already been initialized.' );
+			throw new CKEditorError( 'ui-viewcollection-init-reinit: This ViewCollection has already been initialized.' );
 		}
 
 		const promises = [];
@@ -100,7 +100,9 @@ export default class ViewCollection extends Collection {
 			promises.push( view.init() );
 		}
 
-		return Promise.all( promises );
+		return Promise.all( promises ).then( () => {
+			this.ready = true;
+		} );
 	}
 
 	/**

+ 0 - 17
packages/ckeditor5-ui/tests/view.js

@@ -45,23 +45,6 @@ describe( 'View', () => {
 		} );
 
 		describe( '_viewCollections', () => {
-			it( 'manages #ready attribute binding', () => {
-				const collection = new ViewCollection();
-
-				expect( collection.ready ).to.be.false;
-
-				view._viewCollections.add( collection );
-				expect( collection.ready ).to.be.false;
-
-				view.ready = true;
-				expect( collection.ready ).to.be.true;
-
-				view._viewCollections.remove( collection );
-
-				view.ready = false;
-				expect( collection.ready ).to.be.true;
-			} );
-
 			it( 'manages #locale property', () => {
 				const locale = {
 					t() {}

+ 14 - 0
packages/ckeditor5-ui/tests/viewcollection.js

@@ -76,6 +76,19 @@ describe( 'ViewCollection', () => {
 			expect( collection.init() ).to.be.instanceof( Promise );
 		} );
 
+		it( 'should throw if already initialized', () => {
+			return collection.init()
+				.then( () => {
+					collection.init();
+
+					throw new Error( 'This should not be executed.' );
+				} )
+				.catch( err => {
+					expect( err ).to.be.instanceof( CKEditorError );
+					expect( err.message ).to.match( /ui-viewcollection-init-reinit/ );
+				} );
+		} );
+
 		it( 'calls #init on all views in the collection', () => {
 			const viewA = new View();
 			const viewB = new View();
@@ -98,6 +111,7 @@ describe( 'ViewCollection', () => {
 
 				expect( viewA.element.parentNode ).to.equal( collection._parentElement );
 				expect( viewA.element.nextSibling ).to.equal( viewB.element );
+				expect( collection.ready ).to.be.true;
 			} );
 		} );
 	} );