Closed okerix closed 12 years ago
You have fallen prey to the closure monster.
for (var cc = 0; cc < testForms.length; cc++) {
options.items["fold1"].items[testForms[cc][0]] = {
"name": testForms[cc][0],
callback: function (key, opt) { alert(testForms[cc][1]); }
}
}
that cc is the cause of all your callbacks alerting 22222
. May I suggest something like the following?
var testforms = [
{'Test Name 1': 123456},
{'Test Name 2': 654321},
{'Test Name 3': 22222}
];
for (var cc in testforms) {
options.items["fold1"].items[cc] = {
"name": cc,
callback: function (key, opt) { alert(testForms[key]); }
}
}
Ugh, first time meeting the closure monster. Did some reading up and thanks to your help it works perfect. Thanks! Also great plugin. Easy to work with and efficient!
Not sure if this is an issue or if I'm missing something. I create submenu items when I loop through an array. I try to create the keynames from a field in the array as well as use a value in the array for something in the callback. I think the keyname is not getting set properly because I get the wrong callbacks. If I hard code a basic string keyname it all works perfect. Problem is I don't know how many will be in the submenu. For now I'll just create a big case statement and plan for larger than what I think will be used. Here is an example of my code: