8
0

view.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document, HTMLElement */
  6. /* bender-tags: core, ui */
  7. 'use strict';
  8. const modules = bender.amd.require(
  9. 'ckeditor',
  10. 'ui/view',
  11. 'ui/region',
  12. 'ckeditorerror',
  13. 'model',
  14. 'eventinfo'
  15. );
  16. let View, TestView, Model, Region, CKEditorError;
  17. let view;
  18. bender.tools.createSinonSandbox();
  19. describe( 'View', () => {
  20. beforeEach( updateModuleReference );
  21. describe( 'constructor', () => {
  22. beforeEach( () => {
  23. setTestViewClass();
  24. setTestViewInstance();
  25. } );
  26. it( 'accepts the model', () => {
  27. setTestViewInstance( { a: 'foo', b: 42 } );
  28. expect( view.model ).to.be.an.instanceof( modules.model );
  29. expect( view ).to.have.deep.property( 'model.a', 'foo' );
  30. expect( view ).to.have.deep.property( 'model.b', 42 );
  31. } );
  32. it( 'defines basic view properties', () => {
  33. view = new View();
  34. expect( view.model ).to.be.null;
  35. expect( view.regions.length ).to.be.equal( 0 );
  36. expect( view.template ).to.be.null;
  37. expect( view._regionsSelectors ).to.be.empty;
  38. expect( view._el ).to.be.null;
  39. expect( view._template ).to.be.null;
  40. expect( () => {
  41. view.el;
  42. } ).to.throw( CKEditorError, /ui-view-notemplate/ );
  43. } );
  44. } );
  45. describe( 'init', () => {
  46. beforeEach( () => {
  47. setTestViewClass( () => ( {
  48. tag: 'p',
  49. children: [
  50. { tag: 'span' },
  51. { tag: 'strong' }
  52. ]
  53. } ) );
  54. } );
  55. it( 'calls child regions #init', () => {
  56. setTestViewInstance();
  57. const region1 = new Region( 'x' );
  58. const region2 = new Region( 'y' );
  59. view.register( region1, el => el );
  60. view.register( region2, el => el );
  61. const spy1 = bender.sinon.spy( region1, 'init' );
  62. const spy2 = bender.sinon.spy( region2, 'init' );
  63. view.init();
  64. sinon.assert.calledOnce( spy1 );
  65. sinon.assert.calledOnce( spy2 );
  66. } );
  67. it( 'initializes view regions with string selector', () => {
  68. setTestViewInstance();
  69. const region1 = new Region( 'x' );
  70. const region2 = new Region( 'y' );
  71. view.register( region1, 'span' );
  72. view.register( region2, 'strong' );
  73. view.init();
  74. expect( region1.el ).to.be.equal( view.el.firstChild );
  75. expect( region2.el ).to.be.equal( view.el.lastChild );
  76. } );
  77. it( 'initializes view regions with function selector', () => {
  78. setTestViewInstance();
  79. const region1 = new Region( 'x' );
  80. const region2 = new Region( 'y' );
  81. view.register( region1, el => el.firstChild );
  82. view.register( region2, el => el.lastChild );
  83. view.init();
  84. expect( region1.el ).to.be.equal( view.el.firstChild );
  85. expect( region2.el ).to.be.equal( view.el.lastChild );
  86. } );
  87. it( 'initializes view regions with boolean selector', () => {
  88. setTestViewInstance();
  89. const region1 = new Region( 'x' );
  90. const region2 = new Region( 'y' );
  91. view.register( region1, true );
  92. view.register( region2, true );
  93. view.init();
  94. expect( region1.el ).to.be.null;
  95. expect( region2.el ).to.be.null;
  96. } );
  97. } );
  98. describe( 'addChild', () => {
  99. beforeEach( () => {
  100. setTestViewClass(
  101. () => ( { tag: 'p', text: 'x' } ),
  102. function() {
  103. this.register( 'x', el => el );
  104. }
  105. );
  106. setTestViewInstance();
  107. } );
  108. it( 'should throw when no region name', () => {
  109. expect( () => {
  110. view.addChild();
  111. } ).to.throw( CKEditorError, /ui-view-addchild-badrname/ );
  112. } );
  113. it( 'should throw when region does not exist', () => {
  114. expect( () => {
  115. view.addChild( 'nonexistent' );
  116. } ).to.throw( CKEditorError, /ui-view-addchild-noreg/ );
  117. } );
  118. it( 'should throw when no child view passed', () => {
  119. expect( () => {
  120. view.addChild( 'x' );
  121. } ).to.throw( CKEditorError, /ui-view-addchild-no-view/ );
  122. } );
  123. it( 'should add a child to the region views', () => {
  124. const childView = new View();
  125. view.addChild( 'x', childView );
  126. expect( view.getChild( 'x', 0 ) ).to.be.equal( childView );
  127. } );
  128. it( 'should add a child to the region views at the right index', () => {
  129. const childView1 = new View();
  130. const childView2 = new View();
  131. view.addChild( 'x', childView1 );
  132. view.addChild( 'x', childView2, 0 );
  133. expect( view.getChild( 'x', 0 ) ).to.be.equal( childView2 );
  134. expect( view.getChild( 'x', 1 ) ).to.be.equal( childView1 );
  135. } );
  136. } );
  137. describe( 'removeChild', () => {
  138. beforeEach( () => {
  139. setTestViewClass(
  140. () => ( { tag: 'p', text: 'x' } ),
  141. function() {
  142. this.register( 'x', el => el );
  143. }
  144. );
  145. setTestViewInstance();
  146. } );
  147. it( 'should throw when no region name', () => {
  148. expect( () => {
  149. view.removeChild();
  150. } ).to.throw( CKEditorError, /ui-view-removechild-badrname/ );
  151. } );
  152. it( 'should throw when child view passed', () => {
  153. expect( () => {
  154. view.removeChild( 'x' );
  155. } ).to.throw( CKEditorError, /ui-view-removechild-no-view/ );
  156. } );
  157. it( 'should throw when region does not exist', () => {
  158. expect( () => {
  159. view.removeChild( 'nonexistent', new View() );
  160. } ).to.throw( CKEditorError, /ui-view-removechild-noreg/ );
  161. } );
  162. it( 'should remove a child from the region views', () => {
  163. const childView1 = new View();
  164. const childView2 = new View();
  165. view.addChild( 'x', childView1 );
  166. view.addChild( 'x', childView2, 0 );
  167. expect( view.getChild( 'x', 0 ) ).to.be.equal( childView2 );
  168. expect( view.getChild( 'x', 1 ) ).to.be.equal( childView1 );
  169. view.removeChild( 'x', childView2 );
  170. expect( view.getChild( 'x', 0 ) ).to.be.equal( childView1 );
  171. view.removeChild( 'x', childView1 );
  172. expect( view.regions.get( 'x' ).views.length ).to.be.equal( 0 );
  173. } );
  174. } );
  175. describe( 'getChild', () => {
  176. beforeEach( () => {
  177. setTestViewClass(
  178. () => ( { tag: 'p', text: 'x' } ),
  179. function() {
  180. this.register( 'x', el => el );
  181. }
  182. );
  183. setTestViewInstance();
  184. } );
  185. it( 'should throw when region does not exist', () => {
  186. expect( () => {
  187. view.getChild( 'nonexistent', 0 );
  188. } ).to.throw( CKEditorError, /ui-view-getchild-noreg/ );
  189. } );
  190. it( 'should get a child from the region views', () => {
  191. const childView1 = new View();
  192. const childView2 = new View();
  193. view.addChild( 'x', childView1 );
  194. view.addChild( 'x', childView2, 0 );
  195. expect( view.getChild( 'x', 0 ) ).to.be.equal( childView2 );
  196. expect( view.getChild( 'x', 1 ) ).to.be.equal( childView1 );
  197. } );
  198. } );
  199. describe( 'register', () => {
  200. beforeEach( () => {
  201. setTestViewClass();
  202. setTestViewInstance();
  203. } );
  204. it( 'should throw when first argument is neither Region instance nor string', () => {
  205. expect( () => {
  206. view.register( new Date() );
  207. } ).to.throw( CKEditorError, /ui-view-register-wrongtype/ );
  208. } );
  209. it( 'should throw when missing the selector argument', () => {
  210. expect( () => {
  211. view.register( 'x' );
  212. } ).to.throw( CKEditorError, /ui-view-register-badselector/ );
  213. } );
  214. it( 'should throw when selector argument is of a wrong type', () => {
  215. expect( () => {
  216. view.register( 'x', new Date() );
  217. } ).to.throw( CKEditorError, /ui-view-register-badselector/ );
  218. expect( () => {
  219. view.register( 'x', false );
  220. } ).to.throw( CKEditorError, /ui-view-register-badselector/ );
  221. } );
  222. it( 'should throw when overriding an existing region but without override flag set', () => {
  223. expect( () => {
  224. view.register( 'x', true );
  225. view.register( new Region( 'x' ), true );
  226. } ).to.throw( CKEditorError, /ui-view-register-override/ );
  227. } );
  228. it( 'should register a new region with region name as a first argument', () => {
  229. view.register( 'x', true );
  230. expect( view.regions.get( 'x' ) ).to.be.an.instanceof( Region );
  231. } );
  232. it( 'should register a new region with Region instance as a first argument', () => {
  233. view.register( new Region( 'y' ), true );
  234. expect( view.regions.get( 'y' ) ).to.be.an.instanceof( Region );
  235. } );
  236. it( 'should override an existing region with override flag', () => {
  237. const region1 = new Region( 'x' );
  238. const region2 = new Region( 'x' );
  239. view.register( region1, true );
  240. view.register( region2, true, true );
  241. view.register( 'x', 'span', true );
  242. expect( view.regions.get( 'x' ) ).to.be.equal( region2 );
  243. expect( view._regionsSelectors.x ).to.be.equal( 'span' );
  244. } );
  245. it( 'should not override an existing region with the same region with override flag', () => {
  246. const region = new Region( 'x' );
  247. const spy = bender.sinon.spy( view.regions, 'remove' );
  248. view.register( region, true );
  249. view.register( region, true, true );
  250. sinon.assert.notCalled( spy );
  251. } );
  252. } );
  253. describe( 'el', () => {
  254. beforeEach( createViewInstanceWithTemplate );
  255. it( 'invokes out of #template', () => {
  256. setTestViewInstance( { a: 1 } );
  257. expect( view.el ).to.be.an.instanceof( HTMLElement );
  258. expect( view.el.nodeName ).to.be.equal( 'A' );
  259. } );
  260. it( 'can be explicitly declared', () => {
  261. class CustomView extends View {
  262. constructor() {
  263. super();
  264. this.el = document.createElement( 'span' );
  265. }
  266. }
  267. view = new CustomView();
  268. expect( view.el ).to.be.an.instanceof( HTMLElement );
  269. } );
  270. } );
  271. describe( 'bindToAttribute', () => {
  272. beforeEach( createViewInstanceWithTemplate );
  273. it( 'returns a function that passes arguments', () => {
  274. setTestViewInstance( { a: 'foo' } );
  275. let spy = bender.sinon.spy();
  276. let callback = view.bindToAttribute( 'a', spy );
  277. expect( spy.called ).to.be.false;
  278. callback( 'el', 'updater' );
  279. sinon.assert.calledOnce( spy );
  280. sinon.assert.calledWithExactly( spy, 'el', 'foo' );
  281. view.model.a = 'bar';
  282. sinon.assert.calledTwice( spy );
  283. expect( spy.secondCall.calledWithExactly( 'el', 'bar' ) ).to.be.true;
  284. } );
  285. it( 'allows binding attribute to the model', () => {
  286. setTestViewClass( function() {
  287. return {
  288. tag: 'p',
  289. attrs: {
  290. 'class': this.bindToAttribute( 'foo' )
  291. },
  292. text: 'abc'
  293. };
  294. } );
  295. setTestViewInstance( { foo: 'bar' } );
  296. expect( view.el.outerHTML ).to.be.equal( '<p class="bar">abc</p>' );
  297. view.model.foo = 'baz';
  298. expect( view.el.outerHTML ).to.be.equal( '<p class="baz">abc</p>' );
  299. } );
  300. it( 'allows binding "text" to the model', () => {
  301. setTestViewClass( function() {
  302. return {
  303. tag: 'p',
  304. children: [
  305. {
  306. tag: 'b',
  307. text: 'baz'
  308. }
  309. ],
  310. text: this.bindToAttribute( 'foo' )
  311. };
  312. } );
  313. setTestViewInstance( { foo: 'bar' } );
  314. expect( view.el.outerHTML ).to.be.equal( '<p>bar<b>baz</b></p>' );
  315. // TODO: A solution to avoid nuking the children?
  316. view.model.foo = 'qux';
  317. expect( view.el.outerHTML ).to.be.equal( '<p>qux</p>' );
  318. } );
  319. it( 'allows binding to the model with value processing', () => {
  320. let callback = ( el, value ) =>
  321. ( value > 0 ? 'positive' : 'negative' );
  322. setTestViewClass( function() {
  323. return {
  324. tag: 'p',
  325. attrs: {
  326. 'class': this.bindToAttribute( 'foo', callback )
  327. },
  328. text: this.bindToAttribute( 'foo', callback )
  329. };
  330. } );
  331. setTestViewInstance( { foo: 3 } );
  332. expect( view.el.outerHTML ).to.be.equal( '<p class="positive">positive</p>' );
  333. view.model.foo = -7;
  334. expect( view.el.outerHTML ).to.be.equal( '<p class="negative">negative</p>' );
  335. } );
  336. it( 'allows binding to the model with custom callback', () => {
  337. setTestViewClass( function() {
  338. return {
  339. tag: 'p',
  340. attrs: {
  341. 'class': this.bindToAttribute( 'foo', ( el, value ) => {
  342. el.innerHTML = value;
  343. if ( value == 'changed' ) {
  344. return value;
  345. }
  346. } )
  347. },
  348. text: 'bar'
  349. };
  350. } );
  351. setTestViewInstance( { foo: 'moo' } );
  352. expect( view.el.outerHTML ).to.be.equal( '<p>moo</p>' );
  353. view.model.foo = 'changed';
  354. expect( view.el.outerHTML ).to.be.equal( '<p class="changed">changed</p>' );
  355. } );
  356. } );
  357. describe( 'on', () => {
  358. it( 'accepts plain binding', () => {
  359. let spy = bender.sinon.spy();
  360. setTestViewClass( function() {
  361. return {
  362. tag: 'p',
  363. on: {
  364. x: 'a',
  365. }
  366. };
  367. } );
  368. setTestViewInstance();
  369. view.on( 'a', spy );
  370. dispatchEvent( view.el, 'x' );
  371. sinon.assert.calledWithExactly( spy,
  372. sinon.match.has( 'name', 'a' ),
  373. sinon.match.has( 'target', view.el )
  374. );
  375. } );
  376. it( 'accepts an array of event bindings', () => {
  377. let spy1 = bender.sinon.spy();
  378. let spy2 = bender.sinon.spy();
  379. setTestViewClass( function() {
  380. return {
  381. tag: 'p',
  382. on: {
  383. x: [ 'a', 'b' ]
  384. }
  385. };
  386. } );
  387. setTestViewInstance();
  388. view.on( 'a', spy1 );
  389. view.on( 'b', spy2 );
  390. dispatchEvent( view.el, 'x' );
  391. sinon.assert.calledWithExactly( spy1,
  392. sinon.match.has( 'name', 'a' ),
  393. sinon.match.has( 'target', view.el )
  394. );
  395. sinon.assert.calledWithExactly( spy2,
  396. sinon.match.has( 'name', 'b' ),
  397. sinon.match.has( 'target', view.el )
  398. );
  399. } );
  400. it( 'accepts DOM selectors', () => {
  401. let spy1 = bender.sinon.spy();
  402. let spy2 = bender.sinon.spy();
  403. let spy3 = bender.sinon.spy();
  404. setTestViewClass( function() {
  405. return {
  406. tag: 'p',
  407. children: [
  408. {
  409. tag: 'span',
  410. attrs: {
  411. 'class': 'y',
  412. },
  413. on: {
  414. 'test@p': 'c'
  415. }
  416. },
  417. {
  418. tag: 'div',
  419. children: [
  420. {
  421. tag: 'span',
  422. attrs: {
  423. 'class': 'y',
  424. }
  425. }
  426. ],
  427. }
  428. ],
  429. on: {
  430. 'test@.y': 'a',
  431. 'test@div': 'b'
  432. }
  433. };
  434. } );
  435. setTestViewInstance();
  436. view.on( 'a', spy1 );
  437. view.on( 'b', spy2 );
  438. view.on( 'c', spy3 );
  439. // Test "test@p".
  440. dispatchEvent( view.el, 'test' );
  441. sinon.assert.callCount( spy1, 0 );
  442. sinon.assert.callCount( spy2, 0 );
  443. sinon.assert.callCount( spy3, 0 );
  444. // Test "test@.y".
  445. dispatchEvent( view.el.firstChild, 'test' );
  446. expect( spy1.firstCall.calledWithExactly(
  447. sinon.match.has( 'name', 'a' ),
  448. sinon.match.has( 'target', view.el.firstChild )
  449. ) ).to.be.true;
  450. sinon.assert.callCount( spy2, 0 );
  451. sinon.assert.callCount( spy3, 0 );
  452. // Test "test@div".
  453. dispatchEvent( view.el.lastChild, 'test' );
  454. sinon.assert.callCount( spy1, 1 );
  455. expect( spy2.firstCall.calledWithExactly(
  456. sinon.match.has( 'name', 'b' ),
  457. sinon.match.has( 'target', view.el.lastChild )
  458. ) ).to.be.true;
  459. sinon.assert.callCount( spy3, 0 );
  460. // Test "test@.y".
  461. dispatchEvent( view.el.lastChild.firstChild, 'test' );
  462. expect( spy1.secondCall.calledWithExactly(
  463. sinon.match.has( 'name', 'a' ),
  464. sinon.match.has( 'target', view.el.lastChild.firstChild )
  465. ) ).to.be.true;
  466. sinon.assert.callCount( spy2, 1 );
  467. sinon.assert.callCount( spy3, 0 );
  468. } );
  469. it( 'accepts function callbacks', () => {
  470. let spy1 = bender.sinon.spy();
  471. let spy2 = bender.sinon.spy();
  472. setTestViewClass( function() {
  473. return {
  474. tag: 'p',
  475. children: [
  476. {
  477. tag: 'span'
  478. }
  479. ],
  480. on: {
  481. x: spy1,
  482. 'y@span': [ spy2, 'c' ],
  483. }
  484. };
  485. } );
  486. setTestViewInstance();
  487. dispatchEvent( view.el, 'x' );
  488. dispatchEvent( view.el.firstChild, 'y' );
  489. sinon.assert.calledWithExactly( spy1,
  490. sinon.match.has( 'target', view.el )
  491. );
  492. sinon.assert.calledWithExactly( spy2,
  493. sinon.match.has( 'target', view.el.firstChild )
  494. );
  495. } );
  496. it( 'supports event delegation', () => {
  497. let spy = bender.sinon.spy();
  498. setTestViewClass( function() {
  499. return {
  500. tag: 'p',
  501. children: [
  502. {
  503. tag: 'span'
  504. }
  505. ],
  506. on: {
  507. x: 'a',
  508. }
  509. };
  510. } );
  511. setTestViewInstance();
  512. view.on( 'a', spy );
  513. dispatchEvent( view.el.firstChild, 'x' );
  514. sinon.assert.calledWithExactly( spy,
  515. sinon.match.has( 'name', 'a' ),
  516. sinon.match.has( 'target', view.el.firstChild )
  517. );
  518. } );
  519. it( 'works for future elements', () => {
  520. let spy = bender.sinon.spy();
  521. setTestViewClass( function() {
  522. return {
  523. tag: 'p',
  524. on: {
  525. 'test@div': 'a'
  526. }
  527. };
  528. } );
  529. setTestViewInstance();
  530. view.on( 'a', spy );
  531. let div = document.createElement( 'div' );
  532. view.el.appendChild( div );
  533. dispatchEvent( div, 'test' );
  534. sinon.assert.calledWithExactly( spy, sinon.match.has( 'name', 'a' ), sinon.match.has( 'target', div ) );
  535. } );
  536. } );
  537. describe( 'destroy', () => {
  538. beforeEach( createViewInstanceWithTemplate );
  539. it( 'should destroy the view', () => {
  540. view.destroy();
  541. expect( view.model ).to.be.null;
  542. expect( view.regions ).to.be.null;
  543. expect( view.template ).to.be.null;
  544. expect( view._regionsSelectors ).to.be.null;
  545. expect( view._el ).to.be.null;
  546. expect( view._template ).to.be.null;
  547. } );
  548. it( 'detaches the element from DOM', () => {
  549. const elRef = view.el;
  550. document.createElement( 'div' ).appendChild( view.el );
  551. view.destroy();
  552. expect( elRef.parentNode ).to.be.null;
  553. } );
  554. it( 'destroys child regions', () => {
  555. const region = new Region( 'x' );
  556. const spy = bender.sinon.spy( region, 'destroy' );
  557. const regionsRef = view.regions;
  558. const regionViewsRef = region.views;
  559. view.register( region, true );
  560. view.addChild( 'x', new View() );
  561. view.destroy();
  562. expect( regionsRef.length ).to.be.equal( 0 );
  563. expect( regionViewsRef.length ).to.be.equal( 0 );
  564. expect( spy.calledOnce ).to.be.true;
  565. } );
  566. it( 'detaches bound model listeners', () => {
  567. setTestViewClass( function() {
  568. return {
  569. tag: 'p',
  570. text: this.bindToAttribute( 'foo' )
  571. };
  572. } );
  573. setTestViewInstance( { foo: 'bar' } );
  574. const modelRef = view.model;
  575. const elRef = view.el;
  576. expect( view.el.outerHTML ).to.be.equal( '<p>bar</p>' );
  577. modelRef.foo = 'baz';
  578. expect( view.el.outerHTML ).to.be.equal( '<p>baz</p>' );
  579. view.destroy();
  580. modelRef.foo = 'abc';
  581. expect( elRef.outerHTML ).to.be.equal( '<p>baz</p>' );
  582. } );
  583. it( 'destroy a template–less view', () => {
  584. view = new View();
  585. expect( () => {
  586. view.destroy();
  587. } ).to.not.throw();
  588. } );
  589. } );
  590. } );
  591. function updateModuleReference() {
  592. View = modules[ 'ui/view' ];
  593. Region = modules[ 'ui/region' ];
  594. Model = modules.model;
  595. CKEditorError = modules.ckeditorerror;
  596. }
  597. function createViewInstanceWithTemplate() {
  598. setTestViewClass( () => ( { tag: 'a' } ) );
  599. setTestViewInstance();
  600. }
  601. function setTestViewClass( templateFn, regionsFn ) {
  602. TestView = class V extends View {
  603. constructor( model ) {
  604. super( model );
  605. if ( templateFn ) {
  606. this.template = templateFn.call( this );
  607. }
  608. if ( templateFn && regionsFn ) {
  609. regionsFn.call( this );
  610. }
  611. }
  612. };
  613. }
  614. function setTestViewInstance( model ) {
  615. view = new TestView( new Model( model ) );
  616. if ( view.template ) {
  617. document.body.appendChild( view.el );
  618. }
  619. }
  620. function dispatchEvent( el, domEvtName ) {
  621. if ( !el.parentNode ) {
  622. throw new Error( 'To dispatch an event, element must be in DOM. Otherwise #target is null.' );
  623. }
  624. el.dispatchEvent( new Event( domEvtName, {
  625. bubbles: true
  626. } ) );
  627. }