@@ -286,8 +286,8 @@ export default function generate ( parsed, source, options, names ) {
286
286
const topLevelStatements = [ ] ;
287
287
288
288
const setStatements = [ deindent `
289
- var oldState = state;
290
- state = Object.assign( {}, oldState, newState );
289
+ var oldState = this. state;
290
+ this. state = Object.assign( {}, oldState, newState );
291
291
` ] ;
292
292
293
293
if ( templateProperties . computed ) {
@@ -328,13 +328,13 @@ export default function generate ( parsed, source, options, names ) {
328
328
}
329
329
` ) ;
330
330
331
- setStatements . push ( `applyComputations( state, newState, oldState )` ) ;
331
+ setStatements . push ( `applyComputations( this. state, newState, oldState )` ) ;
332
332
}
333
333
334
334
setStatements . push ( deindent `
335
- dispatchObservers( observers.immediate, newState, oldState );
336
- if ( mainFragment ) mainFragment.update( newState, state );
337
- dispatchObservers( observers.deferred, newState, oldState );
335
+ this. dispatchObservers( this. observers.immediate, newState, oldState );
336
+ if ( this. mainFragment ) this. mainFragment.update( newState, this. state );
337
+ this. dispatchObservers( this. observers.deferred, newState, oldState );
338
338
` ) ;
339
339
340
340
const importBlock = imports
@@ -394,15 +394,15 @@ export default function generate ( parsed, source, options, names ) {
394
394
if ( generator . hasComplexBindings ) {
395
395
initStatements . push ( deindent `
396
396
this.__bindings = [];
397
- var mainFragment = renderMainFragment( state, this );
397
+ this. mainFragment = renderMainFragment( this. state, this );
398
398
if ( options.target ) this.mount( options.target );
399
399
while ( this.__bindings.length ) this.__bindings.pop()();
400
400
` ) ;
401
401
402
402
setStatements . push ( `while ( this.__bindings.length ) this.__bindings.pop()();` ) ;
403
403
} else {
404
404
initStatements . push ( deindent `
405
- var mainFragment = renderMainFragment( state, this );
405
+ this. mainFragment = renderMainFragment( this. state, this );
406
406
if ( options.target ) this.mount( options.target );
407
407
` ) ;
408
408
}
@@ -433,17 +433,25 @@ export default function generate ( parsed, source, options, names ) {
433
433
434
434
topLevelStatements . push ( deindent `
435
435
function ${ constructorName } ( options ) {
436
- var component = this; ${ generator . usesRefs ? `\nthis.refs = {}` : `` }
437
- var state = ${ initialState } ;${ templateProperties . computed ? `\napplyComputations( state, state, {} );` : `` }
436
+ ${ generator . usesRefs ? `\nthis.refs = {}` : `` }
437
+ this. state = ${ initialState } ;${ templateProperties . computed ? `\napplyComputations( this. state, this. state, {} );` : `` }
438
438
439
- var observers = {
439
+ this. observers = {
440
440
immediate: Object.create( null ),
441
441
deferred: Object.create( null )
442
442
};
443
443
444
- var callbacks = Object.create( null );
444
+ this. callbacks = Object.create( null );
445
445
446
- function dispatchObservers ( group, newState, oldState ) {
446
+ this.root = options.root;
447
+ this.yield = options.yield;
448
+
449
+ ${ initStatements . join ( '\n\n' ) }
450
+ }
451
+
452
+ ${ constructorName } .prototype = {
453
+
454
+ dispatchObservers: function dispatchObservers ( group, newState, oldState ) {
447
455
for ( var key in group ) {
448
456
if ( !( key in newState ) ) continue;
449
457
@@ -460,41 +468,41 @@ export default function generate ( parsed, source, options, names ) {
460
468
if ( callback.__calling ) continue;
461
469
462
470
callback.__calling = true;
463
- callback.call( component , newValue, oldValue );
471
+ callback.call( this , newValue, oldValue );
464
472
callback.__calling = false;
465
473
}
466
474
}
467
- }
475
+ },
468
476
469
- this. fire = function fire ( eventName, data ) {
470
- var handlers = eventName in callbacks && callbacks[ eventName ].slice();
477
+ fire: function fire ( eventName, data ) {
478
+ var handlers = eventName in this. callbacks && this. callbacks[ eventName ].slice();
471
479
if ( !handlers ) return;
472
480
473
481
for ( var i = 0; i < handlers.length; i += 1 ) {
474
482
handlers[i].call( this, data );
475
483
}
476
- };
484
+ },
477
485
478
- this. get = function get ( key ) {
479
- return key ? state[ key ] : state;
480
- };
486
+ get: function get ( key ) {
487
+ return key ? this. state[ key ] : this. state;
488
+ },
481
489
482
- this. set = function set ( newState ) {
490
+ set: function set ( newState ) {
483
491
${ setStatements . join ( '\n\n' ) }
484
- };
492
+ },
485
493
486
- this. mount = function mount ( target, anchor ) {
487
- mainFragment.mount( target, anchor );
488
- }
494
+ mount: function mount ( target, anchor ) {
495
+ this. mainFragment.mount( target, anchor );
496
+ },
489
497
490
- this. observe = function ( key, callback, options ) {
491
- var group = ( options && options.defer ) ? observers.deferred : observers.immediate;
498
+ observe: function observe ( key, callback, options ) {
499
+ var group = ( options && options.defer ) ? this. observers.deferred : this. observers.immediate;
492
500
493
501
( group[ key ] || ( group[ key ] = [] ) ).push( callback );
494
502
495
503
if ( !options || options.init !== false ) {
496
504
callback.__calling = true;
497
- callback.call( component, state[ key ] );
505
+ callback.call( this, this. state[ key ] );
498
506
callback.__calling = false;
499
507
}
500
508
@@ -504,10 +512,10 @@ export default function generate ( parsed, source, options, names ) {
504
512
if ( ~index ) group[ key ].splice( index, 1 );
505
513
}
506
514
};
507
- };
515
+ },
508
516
509
- this.on = function on ( eventName, handler ) {
510
- var handlers = callbacks[ eventName ] || ( callbacks[ eventName ] = [] );
517
+ on: function on ( eventName, handler ) {
518
+ var handlers = this. callbacks[ eventName ] || ( this. callbacks[ eventName ] = [] );
511
519
handlers.push( handler );
512
520
513
521
return {
@@ -516,26 +524,22 @@ export default function generate ( parsed, source, options, names ) {
516
524
if ( ~index ) handlers.splice( index, 1 );
517
525
}
518
526
};
519
- };
527
+ },
520
528
521
- this. teardown = function teardown ( detach ) {
529
+ teardown: function teardown ( detach ) {
522
530
this.fire( 'teardown' );${ templateProperties . onteardown ? `\ntemplate.onteardown.call( this );` : `` }
523
531
524
- mainFragment.teardown( detach !== false );
525
- mainFragment = null;
532
+ this. mainFragment.teardown( detach !== false );
533
+ this. mainFragment = null;
526
534
527
- state = {};
528
- };
535
+ this. state = {};
536
+ },
529
537
530
- this.root = options.root;
531
- this.yield = options.yield;
532
-
533
- ${ initStatements . join ( '\n\n' ) }
534
538
}
535
539
` ) ;
536
540
537
541
if ( templateProperties . methods ) {
538
- topLevelStatements . push ( `${ constructorName } .prototype = template.methods;` ) ;
542
+ topLevelStatements . push ( `Object.assign( ${ constructorName } .prototype, template.methods ) ;` ) ;
539
543
}
540
544
541
545
const result = topLevelStatements . join ( '\n\n' ) ;
0 commit comments