I’m trying to get just the keys which are true, to pass to another template. But since they keys are not strings, it seems underscore chokes on it. How to deal with this? I can’t even create a fiddle because it’s not valid JSON.
var keysObj = t.selectedParceIds.keys:
var keys = Object.keys(keysObj);
var trueKeys = keys.reduce(function(result, key){
if (keysObj[key]){
result.push(key);
}
return result;
},[]};
can you help me understand the nature of the keys? So it seems like it’s not a normal JSON doc. I can’t do this for example:
var keysObj = {PP-dev-9dCpuC: "true", PP-dev-xp2wkq: "false", PP-dev-gHct57: "true"};
It’s some kind of special object? Is there no JSON for it?
How, for example, could I run your sample code in JSFIDDLE? I’d need to initialize some keys obj variable because t.selectedParcelIds.keys isn’t available, but I’m not sure how to do that.
Is there some special JavaScripts docs I should read? I don’t remember Object.keys from the books I read.
var keysObj = t.selectedParceIds.keys:
var keys = Object.keys(keysObj);
var trueKeys = keys.reduce(function(result, key){
if (keysObj[key]){
result.push(key);
}
return result;
},[]};
Object.keys(keysObj).forEach(function(key){
if (keysObj[key] === "true"){
trueKeys.push(key);
}
});
Odd that true is a string, but that’s how it seems to work! When you set a ReactiveDict it converts true/false into strings this.selectedParcelIds.set(this.parcel._id, false);
Yes probably, but this is a hack, you are working with the inner parts of reactive-dict.
What if instead of a stroolean (string as a boolean ), you want to test for a “strumber” ? You will have to remember to manually cast ?
reactive-dict.keys is a mix between a string key and a json string representation of the value, but doesn’t seem to be a valid JSON object which you could parse.
IMHO, it should have been _keys or whatsoever, and there should be public, documented, getKeys() which should retrieve an array of key names instead of this hybrid structure.
Time for a PR, i guess…
I think part of the thinking is that they can’t have vars in a reactiveDict which are objects (yet), so they are trying to prevent you from adding an object (i.e. stringify the values)
instead of get() and set() perhaps add() and remove() would have been a better way to go. Along with clear() and item() and containsKey . . . basically how it’s done in .NET But guess we have to work with what we got.
This is what intended, getting the keys and not the values since those “jsonized” values are useless.
It don’t see why my last published code would not work (not tested though).