Skip to content

Commit 4f4a182

Browse files
committed
fix(loki): cloning method for specific classes (#30)
(from techfort/LokiJS#586)
1 parent a8abe99 commit 4f4a182

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

packages/loki/spec/generic/cloning.spec.js

+23
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,29 @@ describe("cloning behavior", () => {
6565
});
6666
});
6767

68+
describe("cloning method \"shallow\" save prototype", function () {
69+
it("works", () => {
70+
function Item(name, owner, maker) {
71+
this.name = name;
72+
this.owner = owner;
73+
this.maker = maker;
74+
}
75+
76+
const cdb = new loki("clonetest");
77+
const citems = cdb.addCollection("items", {clone: true, cloneMethod: "shallow"});
78+
const oldObject = new Item("mjolnir", "thor", "dwarves");
79+
const insObject = citems.insert(oldObject);
80+
81+
// cant' have either of these polluting our collection
82+
oldObject.name = "mewmew";
83+
insObject.name = "mewmew";
84+
85+
const result = citems.findOne({"owner": "thor"});
86+
expect(result instanceof Item).toBe(true);
87+
expect(result.name).toBe("mjolnir");
88+
});
89+
});
90+
6891
describe("collection find() cloning works", () => {
6992
it("works", () => {
7093
const cdb = new loki("cloningEnabled");

packages/loki/src/clone.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ export function clone(data, method) {
1616
break;
1717
case "shallow":
1818
// more compatible method for older browsers
19-
cloned = data.prototype ? Object.create(data.prototype) : {};
19+
cloned = Object.create(data.constructor.prototype);
2020
Object.keys(data).map((i) => {
2121
cloned[i] = data[i];
2222
});
2323
break;
2424
case "shallow-assign":
2525
// should be supported by newer environments/browsers
26-
cloned = data.prototype ? Object.create(data.prototype) : {};
26+
cloned = Object.create(data.constructor.prototype);
2727
Object.assign(cloned, data);
2828
break;
2929
default:

0 commit comments

Comments
 (0)