Skip to content

Commit

Permalink
fix(controller): stop element from inheriting scope
Browse files Browse the repository at this point in the history
  • Loading branch information
jdanyow committed Jan 10, 2016
1 parent 9b61a68 commit a3ced53
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class Controller {
this.isAttached = false;
this.view = null;
this.isBound = false;
this.bindingContext = null;
this.scope = null;

let observerLookup = behavior.observerLocator.getOrCreateObserversLookup(viewModel);
let handlesBind = behavior.handlesBind;
Expand Down Expand Up @@ -71,7 +71,7 @@ export class Controller {
this.view.overrideContext = overrideContext || createOverrideContext(this.viewModel);
this.view._isUserControlled = true;

if(this.behavior.handlesCreated) {
if (this.behavior.handlesCreated) {
this.viewModel.created(owningView || null, this.view);
}

Expand All @@ -90,18 +90,17 @@ export class Controller {
let x;
let observer;
let selfSubscriber;
let context = scope.bindingContext;

if (this.isBound) {
if (this.bindingContext === context) {
if (this.scope === scope) {
return;
}

this.unbind();
}

this.isBound = true;
this.bindingContext = context;
this.scope = scope;

for (i = 0, ii = boundProperties.length; i < ii; ++i) {
x = boundProperties[i];
Expand All @@ -120,14 +119,33 @@ export class Controller {
observer.selfSubscriber = selfSubscriber;
}

let overrideContext;
if (this.view !== null) {
if (skipSelfSubscriber) {
this.view.viewModelScope = scope;
}

this.view.bind(this.viewModel, createOverrideContext(this.viewModel, scope.overrideContext));
// do we need to create an overrideContext or is the scope's overrideContext
// valid for this viewModel?
if (this.viewModel === scope.overrideContext.bindingContext) {
overrideContext = scope.overrideContext;
// should we inherit the parent scope? (eg compose)
} else if (this.instruction.inheritBindingContext) {
overrideContext = createOverrideContext(this.viewModel, scope.overrideContext);
// create the overrideContext and capture the parent without making it
// available to AccessScope. We may need it later for template-part replacements.
} else {
overrideContext = createOverrideContext(this.viewModel);
overrideContext.__parentOverrideContext = scope.overrideContext;
}
this.view.bind(this.viewModel, overrideContext);
} else if (skipSelfSubscriber) {
this.viewModel.bind(context, scope.overrideContext);
overrideContext = scope.overrideContext;
// replacing a template-part? Clone the overrideContext and connect the parent.
if (this.viewModel.viewFactory && this.viewModel.viewFactory.factoryCreateInstruction.partReplacements) {
overrideContext = Object.assign({}, scope.overrideContext);
overrideContext.parentOverrideContext = scope.overrideContext.__parentOverrideContext;
}
this.viewModel.bind(scope.bindingContext, overrideContext);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/instructions.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export class BehaviorInstruction {
instruction.host = host;
instruction.viewModel = viewModel;
instruction.viewFactory = viewFactory;
instruction.inheritBindingContext = true;
return instruction;
}

Expand All @@ -154,6 +155,7 @@ export class BehaviorInstruction {
this.attributes = null;
this.type = null;
this.attrName = null;
this.inheritBindingContext = false;
}
}

Expand Down

0 comments on commit a3ced53

Please sign in to comment.