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 6a30fda
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules/
doc/
dist/**
.idea/
package-lock.json
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 6a30fda

Please sign in to comment.