vendredi 2 janvier 2015

Programmatically access a declarative ToggleButton


I've just started with Dojo/Javascript and even though I kind of understand all the AMD way of coding, I keep getting stuck on these (probably) easy things: how can I programmatically access a widget created in a declarative way?


I have a toggle button created like this:



<button id="toggleLeg" data-dojo-type="dijit/form/ToggleButton" data-dojo-props="iconClass:'dijitCheckBoxIcon', checked: true, onChange: function(value){legendVisibility(value);}">Legend/Layers</button>


And I would like to call the legendVisibility() function when clicking on it using the true/false returning values from the button.


The function simply toggle an accordion container on and off using its CSS property (the accordion is included into a content pane with id="legPanel").



function legendVisibility(value) {
checked = value;
var rightPane = dom.byId('legPanel');
if (checked == false) {
//I want the right pane out
domStyle.set(rightPane , 'display', 'none');
} else {
//I want the right pane in (preserving its content)
domStyle.set(rightPane , 'display', 'block');
}
}


If I put it outside the main require function it clearly throws a ReferenceError saying that "dom is not defined" when declaring the var rightPane.


How can I correctly include it into the require function and programmatically use the widget?


Should I get rid of the onChange: function(value){legendVisibility(value);} when creating the widget?


The modules I need are "dojo/dom" to get the reference of the node and "dojo/dom-style" to change the CSS properties. Do I need the "dojo/on" too to use the event propagated by the click? (I also tried with the Toggler function with no luck!)


Thanks in advance,


mic





Aucun commentaire:

Enregistrer un commentaire