Skip to content

Commit 9c9ef6c

Browse files
committed
Fix bug preventing Refs being serialized properly
Fixes #360
1 parent 6d04416 commit 9c9ef6c

File tree

4 files changed

+7125
-3
lines changed

4 files changed

+7125
-3
lines changed

can/ref/ref-test.js

+69-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var constructor = require("can-connect/constructor/");
66
var canMap = require("can-connect/can/map/");
77
var canRef = require("can-connect/can/ref/");
88
var connect = require("can-connect");
9+
var canSymbol = require("can-symbol");
910

1011
// connects the "raw" data to a a constructor function
1112
// creates ways to CRUD the instances
@@ -249,4 +250,71 @@ QUnit.asyncTest("populate Ref that was already created without a value", functio
249250
QUnit.ok(false, "error");
250251
QUnit.start();
251252
});
252-
});
253+
});
254+
255+
QUnit.test('should serialize object ref', function(){
256+
var Team = DefineMap.extend({
257+
id: 'object'
258+
});
259+
260+
connect([constructor, constructorStore, canMap, canRef],
261+
{
262+
Map: Team
263+
});
264+
265+
var Game = DefineMap.extend({
266+
id: 'string',
267+
teamRef: Team.Ref,
268+
score: "number"
269+
});
270+
271+
connect([constructor, constructorStore, canMap, canRef],
272+
{
273+
Map: Game
274+
});
275+
276+
var team = new Team({id: {_id: 5}});
277+
278+
var game = new Game({
279+
id: 6,
280+
teamRef: team,
281+
score: 22
282+
});
283+
284+
QUnit.equal(typeof game.teamRef.serialize, 'function');
285+
QUnit.equal(typeof game.teamRef[canSymbol.for("can.serialize")], 'function');
286+
QUnit.deepEqual(game.teamRef.serialize(), {_id: 5});
287+
});
288+
289+
290+
QUnit.test('should serialize string ref', function(){
291+
var Team = DefineMap.extend({
292+
id: 'string'
293+
});
294+
295+
connect([constructor, constructorStore, canMap, canRef],
296+
{
297+
Map: Team
298+
});
299+
300+
var Game = DefineMap.extend({
301+
id: 'string',
302+
teamRef: Team.Ref,
303+
score: "number"
304+
});
305+
306+
connect([constructor, constructorStore, canMap, canRef],
307+
{
308+
Map: Game
309+
});
310+
311+
var team = new Team({id: 5});
312+
313+
var game = new Game({
314+
id: 6,
315+
teamRef: team,
316+
score: 22
317+
});
318+
319+
QUnit.equal(game.teamRef.serialize(), '5');
320+
});

can/ref/ref.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ var WeakReferenceMap = require("can-connect/helpers/weak-reference-map");
159159
var Observation = require("can-observation");
160160
var constructorStore = require("can-connect/constructor/store/store");
161161
var define = require("can-define");
162+
var canReflect = require("can-reflect");
163+
var canSymbol = require("can-symbol");
164+
var CIDMap = require("can-cid/map/map");
162165

163166
var makeRef = function(connection){
164167
var idProp = getIdProps(connection)[0];
@@ -385,8 +388,8 @@ var makeRef = function(connection){
385388
* @signature `ref.serialize`
386389
* @return {string} id the id of the referenced instance
387390
*/
388-
Ref.prototype.serialize = function() {
389-
return this[idProp];
391+
Ref.prototype.serialize = Ref.prototype[canSymbol.for("can.serialize")] = function() {
392+
return canReflect.serialize(this[idProp], CIDMap);
390393
};
391394

392395
var baseEventSetup = Ref.prototype._eventSetup;

0 commit comments

Comments
 (0)