Solved! Go to Solution.
Just curious, though, is there a way to call a function outside of its own require statement? I take it making global functions gets messy very quickly, and shouldn't be done, but it's good to know if it is possible to do in the new AMD format.
require(function() { function myFunc() { } // Assign the function to window so it is globally available. window.myFunc = myFunc; });
function updateLayerVisibility (arg1, arg2) { // only need to use require when updateLayerVisibility depends on module1, module2,... require([module1, module2,...], function (module1, module2, ...) { // code for updateLayerVisibility }); }
Thank you for the responses. They are a help.
What I'm trying to do is to pass arguments into a function from various checkboxes (each with their own set of arguments), Is there a way to do that using AMD? Just to explain the situation a bit more:
<script>
require ({.....
function X (takes arguments a, b)
)};
<html section>
checkbox 1 -----function X (argument1a , argument 1b)
checkbox 2 ------ function X (argument2a, argument 2b)
David
What you need to do
- Inside the require function call dojo/on to connect the checkbox to the event handler function. (If you don't need to deal with legacy browsers you should use the built in JavaScript addEventListener function instead of dojo/on.)
define(["dojo/_base/lang", "dojo/dom", "dojo/on", ...], function(lang, dom, on, ...) { return { init: function() { on(dom.byId("chkbox1_ID"), "change", lang.hitch(this, function(evt) { this.chk1_onChange(evt, param1, param2); })); }, chk1_onChange: function(evt, param1, param2) { // respond to the event } }; });
<script> var dojoConfig = { parseOnLoad: true, async: true, packages: [{ name: "module", location: "/path/to/module" }] }; </script> <script src="http://js.arcgis.com/3.6/"></script> <script> require(["module/app", "dojo/domReady!"], function(app) { app.init(); }); </script>
Thanks zj_zou and Jeff,
since I have 8 sets of parameters, I'll end up with 8 event handlers
David
Just curious, though, is there a way to call a function outside of its own require statement? I take it making global functions gets messy very quickly, and shouldn't be done, but it's good to know if it is possible to do in the new AMD format.
require(function() { function myFunc() { } // Assign the function to window so it is globally available. window.myFunc = myFunc; });
I had exactly the same question. Thanks Jeff.