diff --git a/store.js b/store.js index 7ef9b52..97fcfa4 100644 --- a/store.js +++ b/store.js @@ -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 }; }; }; @@ -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 @@ -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) { diff --git a/test/store-test.js b/test/store-test.js index fbb1411..2c40652 100644 --- a/test/store-test.js +++ b/test/store-test.js @@ -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(); + }); +});