Before you ask, a MultiSelect wouldn't really be intuitive for the user in this instance.
I have an object, with an array as its value for 'items', that's provided on the page in a var statement. I also have a function that generates and places dijit.form.FilteringSelect
s and is triggered on a certain button's click event. Every one of these selects should have the aformentioned object as their 'store' attribute. If I write store: new dojo.data.ItemFileReadStore({ data: object_name }),
in my select initialization code, I get weird behavior:
I click the generate button 2 times and get 2 FilteringSelects (call them A,B). I click on B's dropdown piece, and the menu appears attached to B. Then I click on A's dropdown button, and menu appears attached to A. If I then go back and click on B's dropdown button again, nothing happens (well not nothing; an error gets thrown).
However, if instead, I have data: {items: array_literal}
everything works fine, and I switch back and forth between the 2 as much as I want.
As the data:
statement appears once in the javascript code, this isn't a big deal (now that I have a workaround at least). It is however, exceedingly odd. I attempted to dig into the source code, to no avail.
Anybody have any idea what's going on here?
views:
42answers:
1
A:
The cause is if you passed the data
parameter when creating a dojo.data.ItemFileReadStore
. The store uses the object directly. In the source code, you can see this._arrayOfTopLevelItems = dataObject.items;
. So the two stores you created used the same array object as the back-end storage. So a change you made in one widget also changed the other widget. By using array literals, you actually created two distinct objects, then it should be fine. Two widgets will not interfere with each other.
My solution to this case is to use dojo.clone
to create a copy of the data, then use the copy to create the store.
Alex Cheng
2010-05-11 02:25:58
I get that, I just don't understand why activating the menu popup (not even selecting anything) would be a "change"; I guess that If i understood the design decision behind this behavior I might be less confused. Do you have any insight into that?
yarmiganosca
2010-05-11 14:57:10
After the first store is created, its internal structure is correct. But after the second store is created, internal structure of both stores are corrupted. You can use firebug to inspect the store of the two widget and take a look at _arrayOfTopLevelItems object.This kind of change happens when the store is created. I think by using the corrupted store, the widget's behavior is undefined.
Alex Cheng
2010-05-12 06:14:01