Ver código fonte

Merge pull request #87 from ckeditor/t/73

Removed model from the View class.
Aleksander Nowodzinski 9 anos atrás
pai
commit
3f7b4484f3

+ 7 - 6
packages/ckeditor5-ui/src/template.js

@@ -177,10 +177,10 @@ export default class Template {
 			 *			on: {
 			 *				click: [
 			 *					// "clicked" event will be fired on `observableInstance` when "click" fires in DOM.
-			 *					bind( 'clicked' ),
+			 *					bind.to( 'clicked' ),
 			 *
 			 *					// A custom callback function will be executed when "click" fires in DOM.
-			 *					bind( () => {
+			 *					bind.to( () => {
 			 *						...
 			 *					} )
 			 *				]
@@ -195,15 +195,16 @@ export default class Template {
 			 *
 			 * @static
 			 * @method ui.Template.bind#to
-			 * @param {String} attribute Name of {@link utils.ObservableMixin} used in the binding.
+			 * @param {String|Function} eventNameOrFunctionOrAttribute An attribute name of {@link utils.ObservableMixin} or a DOM
+			 * event name or an event callback.
 			 * @param {Function} [callback] Allows processing of the value. Accepts `Node` and `value` as arguments.
 			 * @return {ui.TemplateBinding}
 			 */
-			to( eventNameOrFuncionOrAttribute, callback ) {
+			to( eventNameOrFunctionOrAttribute, callback ) {
 				return {
 					type: bindToSymbol,
-					eventNameOrFunction: eventNameOrFuncionOrAttribute,
-					attribute: eventNameOrFuncionOrAttribute,
+					eventNameOrFunction: eventNameOrFunctionOrAttribute,
+					attribute: eventNameOrFunctionOrAttribute,
 					observable, emitter, callback
 				};
 			},

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

@@ -3,12 +3,12 @@
  * For licensing, see LICENSE.md.
  */
 
-import Model from './model.js';
 import Collection from '../utils/collection.js';
 import Region from './region.js';
 import Template from './template.js';
 import CKEditorError from '../utils/ckeditorerror.js';
 import DOMEmitterMixin from './domemittermixin.js';
+import ObservableMixin from '../utils/observablemixin.js';
 import mix from '../utils/mix.js';
 
 /**
@@ -16,6 +16,7 @@ import mix from '../utils/mix.js';
  *
  * @memberOf ui
  * @mixes DOMEmitterMixin
+ * @mixes ObservableMixin
  */
 export default class View {
 	/**
@@ -25,13 +26,6 @@ export default class View {
 	 */
 	constructor( locale ) {
 		/**
-		 * Model of this view.
-		 *
-		 * @member {ui.Model} ui.View#model
-		 */
-		this.model = new Model();
-
-		/**
 		 * @readonly
 		 * @member {utils.Locale} ui.View#locale
 		 */
@@ -79,10 +73,10 @@ export default class View {
 
 		/**
 		 * Cached {@link ui.Template} binder object specific for this instance.
-		 * See {@link ui.View#bind}.
+		 * See {@link ui.View#bindTemplate}.
 		 *
 		 * @private
-		 * @member {Object} ui.View.#_bind
+		 * @member {Object} ui.View.#_bindTemplate
 		 */
 	}
 
@@ -113,19 +107,18 @@ export default class View {
 	}
 
 	/**
-	 * Shorthand for {@link ui.Template#bind}, bound to {@link ui.View#model}
-	 * and {@link ui.View} on the first access.
+	 * Shorthand for {@link ui.Template#bind}, bound to {@link ui.View} on the first access.
 	 *
-	 * Cached {@link ui.Template#bind} object is stored in {@link ui.View.#_bind}.
+	 * Cached {@link ui.Template#bind} object is stored in {@link ui.View.#_bindTemplate}.
 	 *
-	 * @method ui.View#bind
+	 * @method ui.View#bindTemplate
 	 */
-	get bind() {
-		if ( this._bind ) {
-			return this._bind;
+	get bindTemplate() {
+		if ( this._bindTemplate ) {
+			return this._bindTemplate;
 		}
 
-		return ( this._bind = Template.bind( this.model, this ) );
+		return ( this._bindTemplate = Template.bind( this, this ) );
 	}
 
 	/**
@@ -267,6 +260,7 @@ export default class View {
 }
 
 mix( View, DOMEmitterMixin );
+mix( View, ObservableMixin );
 
 const validSelectorTypes = new Set( [ 'string', 'boolean', 'function' ] );
 

+ 2 - 2
packages/ckeditor5-ui/tests/controllercollection.js

@@ -512,7 +512,7 @@ function defineItemControllerClass() {
 		constructor( model, view ) {
 			super( model, view );
 
-			view.model.bind( 'uid' ).to( model );
+			view.bind( 'uid' ).to( model );
 		}
 	};
 }
@@ -522,7 +522,7 @@ function defineItemViewClass() {
 		constructor( locale ) {
 			super( locale );
 
-			const bind = this.bind;
+			const bind = this.bindTemplate;
 
 			this.template = new Template( {
 				tag: 'li',

+ 4 - 6
packages/ckeditor5-ui/tests/view.js

@@ -122,12 +122,12 @@ describe( 'View', () => {
 		} );
 
 		it( 'returns a shorthand for Template binding', () => {
-			expect( view.bind.to ).to.be.a( 'function' );
-			expect( view.bind.if ).to.be.a( 'function' );
+			expect( view.bindTemplate.to ).to.be.a( 'function' );
+			expect( view.bindTemplate.if ).to.be.a( 'function' );
 
-			const binding = view.bind.to( 'a' );
+			const binding = view.bindTemplate.to( 'a' );
 
-			expect( binding.observable ).to.equal( view.model );
+			expect( binding.observable ).to.equal( view );
 			expect( binding.emitter ).to.equal( view );
 		} );
 	} );
@@ -217,8 +217,6 @@ describe( 'View', () => {
 		it( 'invokes out of #template', () => {
 			setTestViewInstance();
 
-			view.model.set( 'a', 1 );
-
 			expect( view.element ).to.be.an.instanceof( HTMLElement );
 			expect( view.element.nodeName ).to.equal( 'A' );
 		} );