Browse Source

Simplified ComponentFactory class.

Aleksander Nowodziński 9 years ago
parent
commit
a3105084af

+ 7 - 16
packages/ckeditor5-ui/src/componentfactory.js

@@ -45,35 +45,26 @@ export default class ComponentFactory {
 	 * @param {String} name The name of the component.
 	 * @param {Function} ControllerClass The component controller constructor.
 	 * @param {Function} ViewClass The component view constructor.
-	 * @param {ui.Model} model The model of the component.
+	 * @param {Function} [callback] The callback to process the view instance,
+	 * i.e. to set attribute values, create attribute bindings, etc.
 	 */
-	add( name, ControllerClass, ViewClass, model ) {
+	add( name, callback ) {
 		if ( this._components.get( name ) ) {
 			throw new CKEditorError(
 				'componentfactory-item-exists: The item already exists in the component factory.', { name }
 			);
 		}
 
-		this._components.set( name, {
-			ControllerClass,
-			ViewClass,
-			model
-		} );
+		this._components.set( name, callback );
 	}
 
 	/**
-	 * Creates a component instance.
+	 * Creates a component view instance.
 	 *
 	 * @param {String} name The name of the component.
-	 * @returns {ui.Controller} The instantiated component.
+	 * @returns {ui.View} The instantiated component view.
 	 */
 	create( name ) {
-		const component = this._components.get( name );
-
-		const model = component.model;
-		const view = new component.ViewClass( model, this.editor.locale );
-		const controller = new component.ControllerClass( model, view, this.editor );
-
-		return controller;
+		return this._components.get( name )( this.editor.locale );
 	}
 }

+ 9 - 12
packages/ckeditor5-ui/tests/componentfactory.js

@@ -25,33 +25,30 @@ describe( 'ComponentFactory', () => {
 
 	describe( 'add', () => {
 		it( 'throws when trying to override already registered component', () => {
-			factory.add( 'foo', class {}, class {}, {} );
+			factory.add( 'foo', () => {} );
 
 			expect( () => {
-				factory.add( 'foo', class {}, class {}, {} );
+				factory.add( 'foo', () => {} );
 			} ).to.throw( CKEditorError, /^componentfactory-item-exists/ );
 		} );
 	} );
 
 	describe( 'create', () => {
 		it( 'creates an instance', () => {
-			class View {}
-
-			class Controller {
-				constructor( model, view, ed ) {
-					expect( model ).to.equal( model );
-					expect( view ).to.be.instanceof( View );
-					expect( ed ).to.equal( editor );
+			class View {
+				constructor( locale ) {
+					this.locale = locale;
 				}
 			}
 
-			const model = {};
+			const locale = editor.locale = {};
 
-			factory.add( 'foo', Controller, View, model );
+			factory.add( 'foo', locale => new View( locale ) );
 
 			const instance = factory.create( 'foo' );
 
-			expect( instance ).to.be.instanceof( Controller );
+			expect( instance ).to.be.instanceof( View );
+			expect( instance.locale ).to.equal( locale );
 		} );
 	} );
 } );