Przeglądaj źródła

Merge pull request #109 from ckeditor/t/108

Renamed View.addChild() -> View.addChildren() with iterable interface.
Piotrek Koszuliński 9 lat temu
rodzic
commit
552e4fcaa9

+ 18 - 8
packages/ckeditor5-ui/src/view.js

@@ -10,6 +10,7 @@ import DomEmmiterMixin from '../utils/dom/emittermixin.js';
 import ObservableMixin from '../utils/observablemixin.js';
 import Collection from '../utils/collection.js';
 import mix from '../utils/mix.js';
+import isIterable from '../utils/isiterable.js';
 
 /**
  * Basic View class.
@@ -216,18 +217,23 @@ export default class View {
 	 *			constructor( locale ) {
 	 *				super( locale );
 	 *
-	 *				this.childView = new SomeChildView( locale );
+	 *				this.childA = new SomeChildView( locale );
+	 *				this.childB = new SomeChildView( locale );
 	 *
-	 *				// Register a new child view.
-	 *				this.addChild( this.childView );
+	 *				// Register children.
+	 *				this.addChildren( [ this.childA, this.childB ] );
 	 *
 	 *				this.template = new Template( {
 	 *					tag: 'p',
 	 *
 	 *					children: [
+	 *						// This is where the `childA` will render.
+	 *						this.childA,
+	 *
 	 *						{ tag: 'b' },
-	 *						// This is where the `childView` will render.
-	 *						this.childView
+	 *
+	 *						// This is where the `childB` will render.
+	 *						this.childB
 	 *					]
 	 *				} );
 	 *			}
@@ -236,13 +242,17 @@ export default class View {
 	 *		const view = new SampleView( locale );
 	 *
 	 *		view.init().then( () => {
-	 *			// Will append <p><b></b><childView#element></p>
+	 *			// Will append <p><childA#element><b></b><childB#element></p>
 	 *			document.body.appendChild( view.element );
 	 *		} );
 	 *
-	 * @param {...ui.View} children Children views to be registered.
+	 * @param {ui.View|Iterable.<ui.View>} children Children views to be registered.
 	 */
-	addChild( ...children ) {
+	addChildren( children ) {
+		if ( !isIterable( children ) ) {
+			children = [ children ];
+		}
+
 		for ( let child of children ) {
 			this._unboundChildren.add( child );
 		}

+ 7 - 9
packages/ckeditor5-ui/tests/view.js

@@ -82,28 +82,26 @@ describe( 'View', () => {
 		} );
 	} );
 
-	describe( 'addChild', () => {
+	describe( 'addChildren', () => {
 		beforeEach( () => {
 			setTestViewClass();
 			setTestViewInstance();
 		} );
 
-		it( 'should add a view to #_unboundChildren', () => {
+		it( 'should add a single view to #_unboundChildren', () => {
 			expect( view._unboundChildren ).to.have.length( 0 );
 
 			const child = {};
 
-			view.addChild( child );
+			view.addChildren( child );
 			expect( view._unboundChildren ).to.have.length( 1 );
 			expect( view._unboundChildren.get( 0 ) ).to.equal( child );
 		} );
 
-		it( 'should support multiple ...arguments', () => {
+		it( 'should support iterables', () => {
 			expect( view._unboundChildren ).to.have.length( 0 );
 
-			const children = [ {}, {}, {} ];
-
-			view.addChild( ...children );
+			view.addChildren( [ {}, {}, {} ] );
 			expect( view._unboundChildren ).to.have.length( 3 );
 		} );
 	} );
@@ -219,7 +217,7 @@ describe( 'View', () => {
 		it( 'clears #_unboundChildren', () => {
 			const cached = view._unboundChildren;
 
-			view.addChild( new View(), new View() );
+			view.addChildren( new View(), new View() );
 			expect( cached ).to.have.length.above( 1 );
 
 			return view.destroy().then( () => {
@@ -354,5 +352,5 @@ function createViewWithChildren() {
 
 	setTestViewInstance();
 
-	view.addChild( childA, childB );
+	view.addChildren( childA, childB );
 }