PDA

View Full Version : [1.1.0] Id in WebStorageProxy does not need to be int



mitchellsimoens
16 Mar 2011, 8:41 AM
In the WebStorageProxy, the getIds method you parse the id of each record into an integer but why? Why can't you have an id with letters and even symbols?

Override to fix:


Ext.override(Ext.data.WebStorageProxy, {
eliminateDuplicates : function(arr) { //could go into Array prototype (prob better name)
var i = 0,
x = 0,
len = arr.length,
out = [],
obj = {};

for (; i < len; i++) {
obj[arr[i]] = 0;
}

for (i in obj) {
if (Ext.isDefined(i)) {
out.push(i);
}
}

return out;
},

setIds: function(ids) {
if (ids[0] === '') { delete ids[0]; }

ids = this.eliminateDuplicates(ids);
var obj = this.getStorageObject(),
str = ids.join(",");

obj.removeItem(this.id);

if (!Ext.isEmpty(str)) {
obj.setItem(this.id, str);
}
},

getIds: function() {
//first time will be right but after that will return duplicates. Why?
var ids = (this.getStorageObject().getItem(this.id) || "").split(","),
length = ids.length,
i;

return ids;
}
});

mitchellsimoens
23 Apr 2011, 5:09 AM
Just bumping to let people know about my updates I have done to this override.

rez
4 Aug 2011, 4:21 AM
I actually get an undefined id in my localstore using this override (and without the override the 'index' field is set to 4,4,4,4,4,4,4,4,4,4 ... )

I have put a console.log(ids) in the setIds function, producing the output:

app.stores.users2.sync()
["undefined", "4d4f79f2e1054ec23a00000a", "4dea1546e1054e0847000025", "4dea1555e1054e084700002e", "4dea155ae1054e0847000034", "4dea1558e1054e0847000031", "4dea152ae1054e0847000010", "4dea1530e1054e0847000013", "4dea154ce1054e0847000028", "4dea1535e1054e0847000016", "4dea153ae1054e0847000019", "4dea1519e1054e0847000004", "4dea1541e1054e0847000022", "4dea155ee1054e0847000037", "4dea1513e1054e0847000001", "4dea1522e1054e084700000a", "4dea153ee1054e084700001f", "4dea153ce1054e084700001c", "4dea1550e1054e084700002b", "4dea1566e1054e084700003d", "4dea1564e1054e084700003a", "4dea151ee1054e0847000007", "4dea18a8e1054e08470000de", "4dea1524e1054e084700000d", "4d91d7e0e1054e02190001ac", ]

All my user objects do have an id set:

app.stores.users2.first().get('id')
"4d4f79f2e1054ec23a00000a"







Loading the store results in a TypeError

app.stores.users2.load()



TypeError

arguments: Array[2]

0: "id"
1: null
length: 2
__proto__: Array[0]

message: "—"
stack: "—"
type: "non_object_property_load"
__proto__: Error
I can confirm manually deleting the 'undefined' item from the localstorage keys (using the developer tools) will allow the store to load.


I have basically had add a hack to copy the array starting at index 1, I cannot using various methods seem to detect and remove the undefined element. eg (typeof arr[i] == "undefined") etc.

rez
11 Aug 2011, 12:51 AM
underscore.js helped out in cleansing the array of ids:



setIds: function(ids) {
if (ids[0] === '') { delete ids[0]; }
ids = _(ids).chain().compact().uniq().value();


Before using this I still ended up having undefined values even with the override, my method of copying the array to cleanse it of undefined items was horribly slow when it came to a store.proxy.clear() ( 100 objects took ~2mins to clear).