I’m trying to hook up a Select element with a Dojo store. The Select element is declared in HTML and I’m trying to give it a store in some JavaScript code.
It seems the Dojo documentation recommends against this and is in favor of programatically creating the Select element when using a store. However this is a yellow flag to me because I like to keep creation of HTML elements separate from their behavior. In this case, it would be ideal if I could keep the Select element in HTML and hook up the store in JavaScript.
Is the statement in the Dojo docs really the ‘best practice’ for this? I’m looking for opinions from experienced Dojo developers as I’m still getting my feet wet with Dojo.
Intuitively one would use
select.set("store", store)to assign/change store to a dijit as all widgets aredojo/Stateful, but surprisingly it does not work.Anyway there is a method
select.setStore(store, selectedValue, fetchArgs)which (also surprisingly) is not deprecated and works.Define
dijit/form/Selectwithout a store:Assign a store to it:
See it in action at jsFiddle: http://jsfiddle.net/phusick/ZmsYV/
Adding some UX sugar to the aforementioned I would create
dijit/form/Selectdisabledwith single option e.g. Loading… and its final desiredwidth:Then I would enable it after calling
setStore():See this enhanced version at work: http://jsfiddle.net/phusick/xdDEm/