From 6a30fda7aeef11c4df7e6d6c917074f4221aac09 Mon Sep 17 00:00:00 2001 From: imasustink Date: Tue, 10 Oct 2017 20:08:58 -0700 Subject: [PATCH] Fix bug preventing Refs being serialized properly Fixes #360 --- .gitignore | 1 + .npmrc | 1 + can/ref/ref-test.js | 70 ++++++++++++++++++++++++++++++++++++++++++++- can/ref/ref.js | 7 +++-- package.json | 2 ++ 5 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 .npmrc diff --git a/.gitignore b/.gitignore index 9a5137a6..058ac82e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules/ doc/ dist/** .idea/ +package-lock.json diff --git a/.npmrc b/.npmrc new file mode 100644 index 00000000..43c97e71 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/can/ref/ref-test.js b/can/ref/ref-test.js index d4443d77..54196cca 100644 --- a/can/ref/ref-test.js +++ b/can/ref/ref-test.js @@ -6,6 +6,7 @@ var constructor = require("can-connect/constructor/"); var canMap = require("can-connect/can/map/"); var canRef = require("can-connect/can/ref/"); var connect = require("can-connect"); +var canSymbol = require("can-symbol"); // connects the "raw" data to a a constructor function // creates ways to CRUD the instances @@ -249,4 +250,71 @@ QUnit.asyncTest("populate Ref that was already created without a value", functio QUnit.ok(false, "error"); QUnit.start(); }); -}); \ No newline at end of file +}); + +QUnit.test('should serialize object ref', function(){ + var Team = DefineMap.extend({ + id: 'object' + }); + + connect([constructor, constructorStore, canMap, canRef], + { + Map: Team + }); + + var Game = DefineMap.extend({ + id: 'string', + teamRef: Team.Ref, + score: "number" + }); + + connect([constructor, constructorStore, canMap, canRef], + { + Map: Game + }); + + var team = new Team({id: {_id: 5}}); + + var game = new Game({ + id: 6, + teamRef: team, + score: 22 + }); + + QUnit.equal(typeof game.teamRef.serialize, 'function'); + QUnit.equal(typeof game.teamRef[canSymbol.for("can.serialize")], 'function'); + QUnit.deepEqual(game.teamRef.serialize(), {_id: 5}); +}); + + +QUnit.test('should serialize string ref', function(){ + var Team = DefineMap.extend({ + id: 'string' + }); + + connect([constructor, constructorStore, canMap, canRef], + { + Map: Team + }); + + var Game = DefineMap.extend({ + id: 'string', + teamRef: Team.Ref, + score: "number" + }); + + connect([constructor, constructorStore, canMap, canRef], + { + Map: Game + }); + + var team = new Team({id: 5}); + + var game = new Game({ + id: 6, + teamRef: team, + score: 22 + }); + + QUnit.equal(game.teamRef.serialize(), '5'); +}); diff --git a/can/ref/ref.js b/can/ref/ref.js index b18f8467..6df3b0b5 100644 --- a/can/ref/ref.js +++ b/can/ref/ref.js @@ -159,6 +159,9 @@ var WeakReferenceMap = require("can-connect/helpers/weak-reference-map"); var Observation = require("can-observation"); var constructorStore = require("can-connect/constructor/store/store"); var define = require("can-define"); +var canReflect = require("can-reflect"); +var canSymbol = require("can-symbol"); +var CIDMap = require("can-cid/map/map"); var makeRef = function(connection){ var idProp = getIdProps(connection)[0]; @@ -385,8 +388,8 @@ var makeRef = function(connection){ * @signature `ref.serialize` * @return {string} id the id of the referenced instance */ - Ref.prototype.serialize = function() { - return this[idProp]; + Ref.prototype.serialize = Ref.prototype[canSymbol.for("can.serialize")] = function() { + return canReflect.serialize(this[idProp], CIDMap); }; var baseEventSetup = Ref.prototype._eventSetup; diff --git a/package.json b/package.json index db48cea9..c3eb965e 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "main": "can-connect.js", "dependencies": { "can-ajax": "^1.0.6", + "can-cid": "^1.1.1", "can-compute": "^3.3.1", "can-construct": "^3.2.0", "can-define": "^1.2.0", @@ -18,6 +19,7 @@ "can-set": "^1.3.0", "can-stache": "^3.3.0", "can-stache-bindings": "^3.2.0", + "can-symbol": "^1.3.0", "can-types": "^1.1.0", "can-util": "^3.9.5", "can-validate-interface": "0.1.0",