Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small fix in cloning methods #586

Merged
merged 2 commits into from
Jul 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion spec/generic/cloning.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,31 @@ describe('cloning behavior', function () {
expect(result.name).toBe("mjolnir");
});
});


describe('cloning method "shallow" save prototype', function() {
it('works', function() {
function Item(name, owner, maker) {
this.name = name;
this.owner = owner;
this.maker = maker;
}

var cdb = new loki('clonetest');
var citems = cdb.addCollection('items', { clone: true, cloneMethod: "shallow" });
var oldObject = new Item('mjolnir', 'thor', 'dwarves');
var insObject = citems.insert(oldObject);

// cant' have either of these polluting our collection
oldObject.name = 'mewmew';
insObject.name = 'mewmew';

var result = citems.findOne({'owner': 'thor'});

expect(result instanceof Item).toBe(true);
expect(result.name).toBe("mjolnir");
});
});

describe('collection find() cloning works', function() {
it('works', function () {
var cdb = new loki('cloningEnabled');
Expand Down
4 changes: 2 additions & 2 deletions src/lokijs.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,14 +558,14 @@
break;
case "shallow":
// more compatible method for older browsers
cloned = data.prototype?Object.create(data.prototype):{};
cloned = Object.create(data.constructor.prototype);
Object.keys(data).map(function (i) {
cloned[i] = data[i];
});
break;
case "shallow-assign":
// should be supported by newer environments/browsers
cloned = data.prototype?Object.create(data.prototype):{};
cloned = Object.create(data.constructor.prototype);
Object.assign(cloned, data);
break;
default:
Expand Down