@@ -61,7 +61,7 @@ export class Bindable extends DependencyObservable implements definition.Bindabl
6161 }
6262
6363 // if (!types.isNullOrUndefined(bindingSource)) {
64- binding . bind ( bindingSource ) ;
64+ binding . bind ( bindingSource ) ;
6565 // }
6666 }
6767
@@ -119,7 +119,11 @@ export class Bindable extends DependencyObservable implements definition.Bindabl
119119 if ( trace . enabled ) {
120120 trace . write ( `Binding ${ binding . target . get ( ) } .${ binding . options . targetProperty } to new context ${ newValue } ` , trace . categories . Binding ) ;
121121 }
122- binding . bind ( newValue ) ;
122+ if ( ! types . isNullOrUndefined ( newValue ) ) {
123+ binding . bind ( newValue ) ;
124+ } else {
125+ binding . clearBinding ( ) ;
126+ }
123127 }
124128 } ) ;
125129 }
@@ -222,16 +226,18 @@ export class Binding {
222226
223227 source = this . sourceAsObject ( source ) ;
224228
229+ let sourceValue ;
230+
225231 if ( ! types . isNullOrUndefined ( source ) ) {
226232 this . source = new WeakRef ( source ) ;
227233 this . sourceOptions = this . resolveOptions ( source , this . sourceProperties ) ;
228234
229- let sourceValue = this . getSourcePropertyValue ( ) ;
235+ sourceValue = this . getSourcePropertyValue ( ) ;
230236 this . updateTarget ( sourceValue ) ;
231237 this . addPropertyChangeListeners ( this . source , this . sourceProperties ) ;
232- }
233- else {
234- this . updateTarget ( source ) ;
238+ } else if ( ! this . sourceIsBindingContext ) {
239+ sourceValue = this . getSourcePropertyValue ( ) ;
240+ this . updateTarget ( sourceValue ? sourceValue : source ) ;
235241 }
236242 }
237243
@@ -489,7 +495,7 @@ export class Binding {
489495 private getSourcePropertyValue ( ) {
490496 if ( this . options . expression ) {
491497 let changedModel = { } ;
492- changedModel [ bc . bindingValueKey ] = this . source . get ( ) ;
498+ changedModel [ bc . bindingValueKey ] = this . source ? this . source . get ( ) : undefined ;
493499 let expressionValue = this . _getExpressionValue ( this . options . expression , false , changedModel ) ;
494500 if ( expressionValue instanceof Error ) {
495501 trace . write ( ( < Error > expressionValue ) . message , trace . categories . Binding , trace . messageType . error ) ;
@@ -516,6 +522,11 @@ export class Binding {
516522 return null ;
517523 }
518524
525+ public clearBinding ( ) {
526+ this . clearSource ( ) ;
527+ this . updateTarget ( undefined ) ;
528+ }
529+
519530 private updateTarget ( value : any ) {
520531 if ( this . updating ) {
521532 return ;
0 commit comments