瀏覽代碼

Added forEach method to NamedCollection API.

Aleksander Nowodzinski 10 年之前
父節點
當前提交
7262a4e9c9

+ 11 - 0
packages/ckeditor5-engine/src/namedcollection.js

@@ -133,6 +133,17 @@ CKEDITOR.define( [ 'emittermixin', 'ckeditorerror', 'utils' ], function( Emitter
 
 			return model;
 		}
+
+		/**
+		 * Executes the callback for each model in the collection.
+		 *
+		 * @param {Function} callback
+		 * @param {Model} callback.item
+		 * @param {String} callback.name
+		 */
+		forEach( callback ) {
+			this._models.forEach( callback );
+		}
 	}
 
 	utils.extend( NamedCollection.prototype, EmitterMixin );

+ 18 - 0
packages/ckeditor5-engine/tests/mvc/collection/namedcollection.js

@@ -171,6 +171,24 @@ describe( 'remove', function() {
 	} );
 } );
 
+describe( 'forEach', function() {
+	it( 'should iterate over the models', function() {
+		var box = getCollection();
+		var item1 = getItem( 'foo' );
+		var item2 = getItem( 'bar' );
+
+		box.add( item1 );
+		box.add( item2 );
+
+		expect( box ).to.have.length( 2 );
+
+		var spy = sinon.spy();
+		box.forEach( spy );
+
+		sinon.assert.callOrder( spy.withArgs( item1, 'foo' ), spy.withArgs( item2, 'bar' ) );
+	} );
+} );
+
 function getCollection() {
 	var NamedCollection = modules.namedcollection;