Skip to content

Commit

Permalink
Added Single Parent Flag
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanial committed Feb 18, 2018
1 parent 750e38a commit 98cb0cd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/freezer.js
Expand Up @@ -11,7 +11,8 @@ var Freezer = function( initialValue, options ) {
ops = options || {},
store = {
live: ops.live || false,
freezeInstances: ops.freezeInstances || false
freezeInstances: ops.freezeInstances || false,
singleParent: ops.singleParent || false
}
;

Expand Down
3 changes: 3 additions & 0 deletions src/frozen.js
Expand Up @@ -403,6 +403,9 @@ var Frozen = {
;

if( index === -1 ){
if(node.__.store.singleParent && parents.length >= 1){
throw new Error('Node already has a parent');
}
parents[ parents.length ] = parent;
}
},
Expand Down
13 changes: 13 additions & 0 deletions tests/freezer-spec.js
Expand Up @@ -434,4 +434,17 @@ describe("Freezer test", function(){
assert.equal( freezer.get().b.z, 0 );
assert.equal( freezer.get().b.x[0], 'z' );
});

it('singleParent should limit a node to having one parent', function () {
var freezer = new Freezer({
a: {
b: {}
},
c: {}
}, {singleParent: true, live: true});
const oldB = freezer.get().a.b;
assert.throws(function() {
freezer.get().c.set({b: oldB});
}, Error, "Node already has a parent");
});
});

0 comments on commit 98cb0cd

Please sign in to comment.