ソースを参照

Use a proper constructor check. Fixes #113.

Piotrek Koszuliński 9 年 前
コミット
fab6665c80

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

@@ -7,6 +7,7 @@ import CKEditorError from '../utils/ckeditorerror.js';
 import ObservableMixin from '../utils/observablemixin.js';
 import Collection from '../utils/collection.js';
 import mix from '../utils/mix.js';
+import View from './view.js';
 
 /**
  * Collects {@link ui.View} instances.
@@ -233,7 +234,7 @@ export default class ViewCollection extends Collection {
 			as: ( CallbackOrViewClass ) => {
 				let createView;
 
-				if ( CallbackOrViewClass.prototype ) {
+				if ( CallbackOrViewClass.prototype instanceof View ) {
 					createView = ( item ) => {
 						const viewInstance = new CallbackOrViewClass( this.locale, item );
 

+ 23 - 1
packages/ckeditor5-ui/tests/viewcollection.js

@@ -281,7 +281,7 @@ describe( 'ViewCollection', () => {
 				expect( collection ).to.have.length( 0 );
 			} );
 
-			it( 'binds collection as a view factory – custom factory', () => {
+			it( 'binds collection as a view factory – custom factory (arrow function)', () => {
 				const locale = {};
 				const items = new Collection();
 
@@ -301,6 +301,28 @@ describe( 'ViewCollection', () => {
 				expect( collection.get( 0 ).locale ).to.equal( locale );
 				expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
 			} );
+
+			// https://github.com/ckeditor/ckeditor5-ui/issues/113
+			it( 'binds collection as a view factory – custom factory (normal function)', () => {
+				const locale = { locale: true };
+				const items = new Collection();
+
+				collection = new ViewCollection( locale );
+				collection.bindTo( items ).as( function( item ) {
+					return new ViewClass( locale, item );
+				} );
+
+				items.add( { id: '1' } );
+
+				expect( collection ).to.have.length( 1 );
+
+				const view = collection.get( 0 );
+
+				// Wrong args will be passed to the callback if it's treated as the view constructor.
+				expect( view ).to.be.instanceOf( ViewClass );
+				expect( view.locale ).to.equal( locale );
+				expect( view.data ).to.equal( items.get( 0 ) );
+			} );
 		} );
 	} );