Skip to content

Commit

Permalink
fix parent context of component as immediate surrounding context rath…
Browse files Browse the repository at this point in the history
…er than the nearest parent fragment with a set context
  • Loading branch information
evs-chris committed Jan 30, 2019
1 parent 97fe6fd commit cf352a6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/shared/Context.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,16 @@ export default class Context {
getParent(component) {
let fragment = this.fragment;

if (fragment.context)
fragment = findParentWithContext(fragment.parent || (component && fragment.componentParent));
if (!fragment.parent && component) fragment = fragment.componentParent;
else {
fragment = findParentWithContext(fragment.parent || (component && fragment.componentParent));
if (fragment)
fragment = findParentWithContext(
fragment.parent || (component && fragment.componentParent)
);
if (fragment.context) fragment = findParentWithContext(fragment.parent);
else {
fragment = findParentWithContext(fragment.parent);
if (fragment) {
if (!fragment.parent && component) fragment = fragment.componentParent;
else fragment = findParentWithContext(fragment.parent);
}
}
}

if (!fragment || fragment === this.fragment) return;
Expand Down
18 changes: 18 additions & 0 deletions tests/browser/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -1251,4 +1251,22 @@ export default function() {
t.equal(ctx.findAllComponents()[1].get('id'), 'second');
t.equal(ctx.findAllComponents()[2].get('id'), 'other');
});

test('parent context of ractive context for a component is the context immediately surrounding the component', t => {
const r = new Ractive({
target: fixture,
template: '{{#with 99 as bar}}<cmp />{{/with}}',
components: {
cmp: Ractive.extend()
},
data: {
bar: 42
}
});

const cmp = r.findComponent('cmp');
const ctx = cmp.getContext().getParent(true);

t.equal(ctx.get('bar'), 99);
});
}

0 comments on commit cf352a6

Please sign in to comment.