Browse Source

Destroy View and its regions.

Aleksander Nowodzinski 10 years ago
parent
commit
b686f55cce

+ 1 - 1
packages/ckeditor5-engine/src/ui/region.js

@@ -47,7 +47,7 @@ CKEDITOR.define( [ 'collection', 'model' ], function( Collection, Model ) {
 			// Drop the reference to HTMLElement.
 			this.el = null;
 
-			// Remove views.
+			// Remove and destroy views.
 			for ( let i = this.views.length; i--; ) {
 				this.views.remove( i ).destroy();
 			}

+ 14 - 1
packages/ckeditor5-engine/src/ui/view.js

@@ -126,7 +126,20 @@ CKEDITOR.define( [
 			return this._template.render();
 		}
 
-		destroy() {}
+		destroy() {
+			// Drop the reference to the model.
+			this.model = null;
+
+			// Remove View's element from DOM.
+			if ( this.template ) {
+				this.el.remove();
+			}
+
+			// Remove and destroy regions.
+			for ( let i = this.regions.length; i--; ) {
+				this.regions.remove( i ).destroy();
+			}
+		}
 	}
 
 	return View;

+ 27 - 2
packages/ckeditor5-engine/tests/ui/view.js

@@ -7,13 +7,16 @@
 
 'use strict';
 
-var modules = bender.amd.require( 'ckeditor', 'ui/view', 'ckeditorerror' );
+var modules = bender.amd.require( 'ckeditor', 'ui/view', 'ui/region', 'ckeditorerror' );
+
+bender.tools.createSinonSandbox();
 
 describe( 'View', function() {
 	var view;
+	var View;
 
 	beforeEach( 'Create a test view instance', function() {
-		var View = modules[ 'ui/view' ];
+		View = modules[ 'ui/view' ];
 
 		view = new View( {
 			a: 'foo',
@@ -53,4 +56,26 @@ describe( 'View', function() {
 		sinon.assert.calledOnce( spy );
 		sinon.assert.calledWithExactly( spy, 'el', 'bar' );
 	} );
+
+	it( 'is destroyed properly', function() {
+		var Region = modules[ 'ui/region' ];
+		var region = new Region();
+		var spy = bender.sinon.spy( region, 'destroy' );
+
+		class TestView extends View {
+			constructor() {
+				super();
+				this.template = { tag: 'a' };
+			}
+		}
+
+		view = new TestView();
+
+		view.regions.add( region );
+		view.destroy();
+
+		expect( view.el.parentNode ).to.be.null;
+		expect( view.regions ).to.have.length( 0 );
+		expect( spy.calledOnce ).to.be.true;
+	} );
 } );