Skip to content

Commit

Permalink
Merge pull request #174 from canjs/idprop-string
Browse files Browse the repository at this point in the history
Correctly handle createData with string ids
  • Loading branch information
matthewp authored Jul 8, 2019
2 parents 7f28139 + 83231d4 commit d70c13c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
24 changes: 19 additions & 5 deletions store.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,22 @@ var makeMakeItems = function(baseItems, idProp){
return function () {
// clone baseItems
var items = [],
maxId = 0;
maxId = 0,
idType = "number";
baseItems.forEach(function(item){
items.push(canReflect.serialize(item) );
maxId = Math.max(item[idProp], maxId) ;
var type = typeof item[idProp];
if(type === "number") {
maxId = Math.max(item[idProp], maxId) ;
} else {
idType = type;
}
});

return {
maxId: maxId,
items: items
items: items,
idType: idType
};
};
};
Expand Down Expand Up @@ -82,12 +89,18 @@ var doNotConvert = function(v){ return v; };

function typeConvert(data){
var schema = this.connection.queryLogic.schema;
var idType = this.idType;
var identityKey = schema.identity[0],
keys = schema.keys;
if(!keys || !keys[identityKey]) {
keys = {};
keys[identityKey] = function(value){
return typeof value === "string" ? stringToAny(value) : value;
keys[identityKey] = function(value) {
if(idType === "string") {
return ""+value;
} else {
return typeof value === "string" ? stringToAny(value) : value;
}

};
}
// this probably needs to be recursive, but this is ok for now
Expand Down Expand Up @@ -142,6 +155,7 @@ canReflect.assignMap(Store.prototype,{
}
var itemData = this.makeItems();
this.maxId = itemData.maxId;
this.idType = itemData.idType;
this.connection.updateListData(itemData.items, {});
},
get: function (params) {
Expand Down
14 changes: 14 additions & 0 deletions test/store-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,17 @@ QUnit.test("createData, destroyData, updateData", function(assert){
done();
});*/
});

QUnit.test("createData with a string id", function(assert){
var store = fixture.store([
{id: "helloorld", name: "foo"}
], new QueryLogic({identity: ["id"]}));

var done = assert.async();
store.createData({
data: {name: "bar"}
}, function(instance){
assert.deepEqual(instance, {id: "1", name: "bar"} );
done();
});
});

0 comments on commit d70c13c

Please sign in to comment.