Skip to content

Commit

Permalink
fix a few corner cases on automatic instance data linking
Browse files Browse the repository at this point in the history
  • Loading branch information
evs-chris committed Jan 25, 2019
1 parent 44f27ce commit 8455933
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/model/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ export default class Model extends ModelBase {
this.adapt();
}

if (this.dataModel) checkDataLink(this, value);
if (this.dataModel || (value && value.viewmodel && value.viewmodel.isRoot)) {
checkDataLink(this, value);
}

// keep track of array stuff
if (isArray(value)) {
Expand Down Expand Up @@ -259,7 +261,9 @@ export default class Model extends ModelBase {
const old = this.value;
const value = this.retrieve();

if (this.dataModel) checkDataLink(this, value);
if (this.dataModel || (value && value.viewmodel && value.viewmodel.isRoot)) {
checkDataLink(this, value);
}

if (force || !isEqual(value, old)) {
this.value = value;
Expand Down
7 changes: 4 additions & 3 deletions src/model/ModelBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,12 +363,13 @@ export function shuffle(model, newIndices, link, unsafe) {
}

export function checkDataLink(model, value) {
if (model.dataModel && value !== model.dataModel) {
if (value && value.viewmodel && value.viewmodel.isRoot) {
if (value !== model.dataModel) {
if (value && value.viewmodel && value.viewmodel.isRoot && model.childByKey.data) {
model.childByKey.data.link(value.viewmodel, 'data');
model.dataModel = value;
} else {
} else if (model.dataModel) {
model.childByKey.data.unlink();
model.dataModel = true;
}
}
}
48 changes: 48 additions & 0 deletions tests/browser/references.js
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,23 @@ export default function() {

r.set('child', c2);
t.equal(fixture.innerHTML, '99');

c2.set('what', 33);
t.equal(fixture.innerHTML, '33');

r.set('child', null);
t.equal(fixture.innerHTML, '');

r.set('child', c2);
t.equal(fixture.innerHTML, '33');

r.set('child', null);
t.equal(fixture.innerHTML, '');

r.set('child', c1);
t.equal(fixture.innerHTML, '42');
r.set('child', c2);
t.equal(fixture.innerHTML, '33');
});

test(`automatic linking of instance data in an each`, t => {
Expand All @@ -818,4 +835,35 @@ export default function() {
r.unshift('children', c3);
t.equal(fixture.innerHTML, '2|99|42|');
});

test(`automatic linking with child component and data function`, t => {
const cmp = Ractive.extend({
data() {
return { what: 42 };
}
});

new Ractive({
target: fixture,
template: `<cmp on-init="@.set('@.cmp', $1)" />{{@.cmp.data.what}}`,
components: { cmp }
});

t.htmlEqual(fixture.innerHTML, '42');
});

test(`automatic instance linking with oustide updates`, t => {
const c1 = new Ractive({ data: { what: 42 } });

const r = new Ractive({
target: fixture,
template: '{{@.child.data.what}}'
});

t.htmlEqual(fixture.innerHTML, '');

r.child = c1;
r.update('@.child');
t.htmlEqual(fixture.innerHTML, '42');
});
}

0 comments on commit 8455933

Please sign in to comment.