Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug preventing Refs being serialized properly #361

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a good reason to provide a Map here? Seems to me like it would only be good for circular reference, which shouldn't happen here.

};

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