Sfoglia il codice sorgente

Collection#clear breaks #bindTo binding.

Aleksander Nowodzinski 8 anni fa
parent
commit
06f26fcaaa

+ 9 - 1
packages/ckeditor5-utils/src/collection.js

@@ -292,9 +292,15 @@ export default class Collection {
 	}
 
 	/**
-	 * Removes all items from the collection.
+	 * Removes all items from the collection and destroys the binding created using
+	 * {@link #bindTo}.
 	 */
 	clear() {
+		if ( this._bindToCollection ) {
+			this.stopListening( this._bindToCollection );
+			this._bindToCollection = null;
+		}
+
 		while ( this.length ) {
 			this.remove( 0 );
 		}
@@ -372,6 +378,8 @@ export default class Collection {
 	 *		console.log( target.get( 0 ).value ); // 'foo'
 	 *		console.log( target.get( 1 ).value ); // 'bar'
 	 *
+	 * **Note**: {@link #clear} can be used to break the binding.
+	 *
 	 * @param {module:utils/collection~Collection} collection A collection to be bound.
 	 * @returns {Object}
 	 * @returns {module:utils/collection~Collection#bindTo#as} return.as

+ 18 - 0
packages/ckeditor5-utils/tests/collection.js

@@ -508,6 +508,24 @@ describe( 'Collection', () => {
 			expect( spy.callCount ).to.equal( 3 );
 			expect( collection.length ).to.equal( 0 );
 		} );
+
+		it( 'breaks the binding', () => {
+			const external = new Collection();
+			collection.bindTo( external ).using( i => i );
+
+			external.add( { foo: 'bar' } );
+			expect( collection ).to.have.length( 1 );
+
+			collection.clear();
+
+			external.add( { foo: 'baz' } );
+			expect( collection ).to.have.length( 0 );
+
+			external.remove( 0 );
+			expect( collection ).to.have.length( 0 );
+
+			expect( collection._bindToCollection ).to.be.null;
+		} );
 	} );
 
 	describe( 'bindTo()', () => {