Skip to content

Commit

Permalink
make sure unlinking and relinking properly invalidates deps
Browse files Browse the repository at this point in the history
  • Loading branch information
evs-chris committed Jun 26, 2017
1 parent 1ebd8aa commit 6dc0473
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/model/LinkModel.js
Expand Up @@ -154,7 +154,10 @@ export default class LinkModel extends ModelBase {

if ( this.rootLink ) this.addShuffleTask( () => {
this.relinked();
if ( !safe ) this.notifyUpstream();
if ( !safe ) {
this.markedAll();
this.notifyUpstream();
}
});
}

Expand Down Expand Up @@ -193,7 +196,7 @@ ModelBase.prototype.link = function link ( model, keypath, options ) {
lnk.implicit = options && options.implicit;
lnk.sourcePath = keypath;
lnk.rootLink = true;
if ( this._link ) this._link.relinking( model, true, false );
if ( this._link ) this._link.relinking( model, false );
this.rebind( lnk, this, false );
fireShuffleTasks();

Expand All @@ -208,7 +211,7 @@ ModelBase.prototype.unlink = function unlink () {
if ( this._link ) {
const ln = this._link;
this._link = undefined;
ln.rebind( this, this._link );
ln.rebind( this, ln, false );
fireShuffleTasks();
ln.teardown();
this.notifyUpstream();
Expand Down
27 changes: 27 additions & 0 deletions tests/browser/methods/link.js
Expand Up @@ -160,4 +160,31 @@ export default function() {
r2.link( 'foo.bar', 'foo.bar', { ractive: r1 });
}, /keypath cannot be linked to itself/i);
});

test( `unlinking and relinking linked links properly updates deps`, t => {
const r1 = new Ractive({
data: { foo: { bar: 'a' }, bat: { bar: 'b' } }
});
const r2 = new Ractive({
target: fixture,
template: '{{~/link.bar}}'
});

r2.link( 'baz', 'link', { ractive: r1 } );
r1.link( 'foo', 'baz' );

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

r1.unlink( 'baz' );
t.equal( fixture.innerHTML, '' );

r1.link( 'bat', 'baz' );
t.htmlEqual( fixture.innerHTML, 'b' );

r1.unlink( 'baz' );
t.equal( fixture.innerHTML, '' );

r1.link( 'bat', 'baz' );
t.htmlEqual( fixture.innerHTML, 'b' );
});
}

0 comments on commit 6dc0473

Please sign in to comment.