From 606667eb4dc34979e86180695ce245647c19f617 Mon Sep 17 00:00:00 2001 From: Henry Date: Tue, 17 Nov 2015 14:30:34 -0800 Subject: [PATCH 1/4] [bug] TypeImpl.getInitialmodel should always return a fresh (ie. cloned) model. #316 --- platform/core/src/types/TypeImpl.js | 2 +- platform/core/test/types/TypeImplSpec.js | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/platform/core/src/types/TypeImpl.js b/platform/core/src/types/TypeImpl.js index deb23873e5..1a9854ea74 100644 --- a/platform/core/src/types/TypeImpl.js +++ b/platform/core/src/types/TypeImpl.js @@ -157,7 +157,7 @@ define( }; TypeImpl.prototype.getInitialModel = function () { - return this.typeDef.model || {}; + return JSON.parse(JSON.stringify(this.typeDef.model || {})); }; TypeImpl.prototype.getDefinition = function () { diff --git a/platform/core/test/types/TypeImplSpec.js b/platform/core/test/types/TypeImplSpec.js index d29c4f2712..f0a89875b3 100644 --- a/platform/core/test/types/TypeImplSpec.js +++ b/platform/core/test/types/TypeImplSpec.js @@ -101,6 +101,10 @@ define( expect(type.getInitialModel().someKey).toEqual("some value"); }); + it("provides a fresh initial model each time", function () { + expect(type.getInitialModel().someKey).toEqual("some value"); + }); + it("provides type properties", function () { expect(type.getProperties().length).toEqual(1); }); From 3a363898156bdd506d4c6e40de0e24c4ad6edf28 Mon Sep 17 00:00:00 2001 From: Henry Date: Tue, 17 Nov 2015 14:45:10 -0800 Subject: [PATCH 2/4] Fixed test --- platform/core/test/types/TypeImplSpec.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/platform/core/test/types/TypeImplSpec.js b/platform/core/test/types/TypeImplSpec.js index f0a89875b3..ac5ee2b0d7 100644 --- a/platform/core/test/types/TypeImplSpec.js +++ b/platform/core/test/types/TypeImplSpec.js @@ -102,6 +102,8 @@ define( }); it("provides a fresh initial model each time", function () { + var model = type.getInitialModel(); + model.someKey = "some other value" expect(type.getInitialModel().someKey).toEqual("some value"); }); From 40b21e35fd9e861bcf561404cf715cb20a6f9a92 Mon Sep 17 00:00:00 2001 From: Henry Date: Tue, 17 Nov 2015 15:03:17 -0800 Subject: [PATCH 3/4] Fixed jslint error --- platform/core/test/types/TypeImplSpec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform/core/test/types/TypeImplSpec.js b/platform/core/test/types/TypeImplSpec.js index ac5ee2b0d7..0203058934 100644 --- a/platform/core/test/types/TypeImplSpec.js +++ b/platform/core/test/types/TypeImplSpec.js @@ -103,7 +103,7 @@ define( it("provides a fresh initial model each time", function () { var model = type.getInitialModel(); - model.someKey = "some other value" + model.someKey = "some other value"; expect(type.getInitialModel().someKey).toEqual("some value"); }); From 5ced8e655d46e1238de838f4e1855b57639bfd10 Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 18 Nov 2015 12:14:00 -0800 Subject: [PATCH 4/4] [bug] #317 Added jsdoc note about performance --- platform/core/src/types/TypeImpl.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/platform/core/src/types/TypeImpl.js b/platform/core/src/types/TypeImpl.js index 1a9854ea74..333f37663f 100644 --- a/platform/core/src/types/TypeImpl.js +++ b/platform/core/src/types/TypeImpl.js @@ -156,6 +156,13 @@ define( }); }; + /** + * Returns the default model for an object of this type. Note that + * this method returns a clone of the original model, so if using this + * method heavily, consider caching the result to optimize performance. + * + * @return {object} The default model for an object of this type. + */ TypeImpl.prototype.getInitialModel = function () { return JSON.parse(JSON.stringify(this.typeDef.model || {})); };