Skip to content

Commit

Permalink
do a shuffle notification when a reference expression changes paths
Browse files Browse the repository at this point in the history
  • Loading branch information
evs-chris committed May 18, 2020
1 parent f150364 commit b311a69
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/model/LinkModel.js
Expand Up @@ -156,6 +156,8 @@ export default class LinkModel extends ModelBase {
c.relinking(target.joinKey(c.key), safe);
});

if (!safe) this.keypath = undefined;

if (this.rootLink)
this.addShuffleTask(() => {
this.relinked();
Expand Down
7 changes: 6 additions & 1 deletion src/view/items/Element.js
Expand Up @@ -6,7 +6,7 @@ import { escapeHtml, voidElements } from 'utils/html';
import { createElement, detachNode, matches, safeAttributeString } from 'utils/dom';
import runloop from 'src/global/runloop';
import Context from 'shared/Context';
import { destroyed } from 'shared/methodCallers';
import { destroyed, shuffled } from 'shared/methodCallers';
import { ContainerItem } from './shared/Item';
import Fragment from '../Fragment';
import ConditionalAttribute from './element/ConditionalAttribute';
Expand Down Expand Up @@ -380,6 +380,11 @@ export default class Element extends ContainerItem {
this.rendered = true;
}

shuffled() {
super.shuffled();
this.decorators.forEach(shuffled);
}

toString() {
const tagName = this.template.e;

Expand Down
4 changes: 4 additions & 0 deletions src/view/items/element/Decorator.js
Expand Up @@ -94,6 +94,10 @@ export default class Decorator {
}, true);
}

shuffled() {
if (this.handle && this.handle.shuffled) this.handle.shuffled();
}

toString() {
return '';
}
Expand Down
1 change: 1 addition & 0 deletions src/view/resolvers/ReferenceExpressionProxy.js
Expand Up @@ -98,6 +98,7 @@ export default class ReferenceExpressionProxy extends LinkModel {
this.relinking(model);
fireShuffleTasks();
refreshPathDeps(this);
this.fragment.shuffled();
}
};

Expand Down
36 changes: 36 additions & 0 deletions tests/browser/plugins/decorators.js
Expand Up @@ -739,4 +739,40 @@ export default function() {
target: fixture
});
});

test(`decorators are notified of shuffling due to reference expression changes`, t => {
t.expect(4);

let path, child;
const r = new Ractive({
target: fixture,
template: `{{#with foo[bar]}}<div as-check />{{/with}}`,
data: {
bar: 'baz',
foo: {
baz: {},
bat: {}
}
},
decorators: {
check(node) {
const ctx = this.getContext(node);
path = ctx.resolve();
child = ctx.resolve('.foo');
return {
teardown() {},
shuffled() {
path = ctx.resolve();
child = ctx.resolve('.foo');
}
};
}
}
});
t.equal(path, 'foo.baz');
t.equal(child, 'foo.baz.foo');
r.set('bar', 'bat');
t.equal(path, 'foo.bat');
t.equal(child, 'foo.bat.foo');
});
}

0 comments on commit b311a69

Please sign in to comment.