Skip to content

Commit

Permalink
Fix bug preventing Refs being serialized properly
Browse files Browse the repository at this point in the history
Fixes #360
  • Loading branch information
imaustink committed Oct 11, 2017
1 parent 6d04416 commit 9c9ef6c
Show file tree
Hide file tree
Showing 4 changed files with 7,125 additions and 3 deletions.
70 changes: 69 additions & 1 deletion can/ref/ref-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -249,4 +250,71 @@ QUnit.asyncTest("populate Ref that was already created without a value", functio
QUnit.ok(false, "error");
QUnit.start();
});
});
});

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');
});
7 changes: 5 additions & 2 deletions can/ref/ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 9c9ef6c

Please sign in to comment.