dojo.provide("utilities.common"); utilities.common.getType = function(value) { var type = "config.json"; switch (value) { case "test": type = "config-test.json"; break case "landdevelopment": type = "config-landdevelopment.json"; break case "utilitiesinfrastructure": type = "config-utilitiesinfrastructure.json"; break default: type = "config.json"; } return type; }; utilities.common.getFormattedServiceType = function(value) { var type = "Property Locator"; var newValue = (value.replace(".json", "")).replace('config-', ''); switch (newValue) { case "test": type = "Test"; break case "landdevelopment": type = "Land Development"; break case "utilitiesinfrastructure": type = "Utilities Infrastructure"; break default: type = "Property Locator"; } return type; }; utilities.common.getAllNamedChildDijits = function(id) { // Gather all child widgets var w = null; var children = null; widgets = {}; w = dojo.query("[widgetId]", dojo.byId(id)); children = w.map(dijit.byNode); children.forEach(function(item, idx) { if (item.name) { widgets[item.name] = item; } }); return widgets; };
define({ getType: function(value) { var type = "config.json"; switch (value) { case "test": type = "config-test.json"; break case "landdevelopment": type = "config-landdevelopment.json"; break case "utilitiesinfrastructure": type = "config-utilitiesinfrastructure.json"; break default: type = "config.json"; } return type; }, getFormattedServiceType: function(value) { var type = "Property Locator"; var newValue = (value.replace(".json", "")).replace('config-', ''); switch (newValue) { case "test": type = "Test"; break case "landdevelopment": type = "Land Development"; break case "utilitiesinfrastructure": type = "Utilities Infrastructure"; break default: type = "Property Locator"; } return type; }, getAllNamedChildDijits: function(id) { // Gather all child widgets var w = null; var children = null; widgets = {}; w = dojo.query("[widgetId]", dojo.byId(id)); children = w.map(dijit.byNode); children.forEach(function(item, idx) { if (item.name) { widgets[item.name] = item; } }); return widgets; } });
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=7, IE=9, IE=10"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no"> <title></title> <link rel="stylesheet" href="https://community.esri.com//js.arcgis.com/3.6/js/esri/css/esri.css"> <style> html, body, #map { height: 100%; width: 100%; margin: 0; padding: 0; } </style> <script> var dojoConfig = { paths: { utilities: location.pathname.replace(/\/[^/]+$/, "") } } </script> <script src="//js.arcgis.com/3.6/"></script> <script> var map; require([ "esri/map", "utilities/common", "dojo/domReady!" ], function( Map, common ) { map = new Map("map", { basemap: "streets", center: [ 19.291, 48.343 ], zoom: 4 }); console.log("common", common); }); </script> </head> <body> <div id="map"></div> </body> </html>
You can pass an object to define() that defines your utility functions. Put this in common.js:define({ getType: function(value) { var type = "config.json"; switch (value) { case "test": type = "config-test.json"; break case "landdevelopment": type = "config-landdevelopment.json"; break case "utilitiesinfrastructure": type = "config-utilitiesinfrastructure.json"; break default: type = "config.json"; } return type; }, getFormattedServiceType: function(value) { var type = "Property Locator"; var newValue = (value.replace(".json", "")).replace('config-', ''); switch (newValue) { case "test": type = "Test"; break case "landdevelopment": type = "Land Development"; break case "utilitiesinfrastructure": type = "Utilities Infrastructure"; break default: type = "Property Locator"; } return type; }, getAllNamedChildDijits: function(id) { // Gather all child widgets var w = null; var children = null; widgets = {}; w = dojo.query("[widgetId]", dojo.byId(id)); children = w.map(dijit.byNode); children.forEach(function(item, idx) { if (item.name) { widgets[item.name] = item; } }); return widgets; } });
Put this in load-common.html in the same directory as common.js:<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=7, IE=9, IE=10"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no"> <title></title> <link rel="stylesheet" href="https://community.esri.com//js.arcgis.com/3.6/js/esri/css/esri.css"> <style> html, body, #map { height: 100%; width: 100%; margin: 0; padding: 0; } </style> <script> var dojoConfig = { paths: { utilities: location.pathname.replace(/\/[^/]+$/, "") } } </script> <script src="//js.arcgis.com/3.6/"></script> <script> var map; require([ "esri/map", "utilities/common", "dojo/domReady!" ], function( Map, common ) { map = new Map("map", { basemap: "streets", center: [ 19.291, 48.343 ], zoom: 4 }); console.log("common", common); }); </script> </head> <body> <div id="map"></div> </body> </html>
define(["dojo/dom", "dojo/query"], function(dom, query) { getType: function(value) { var type = "config.json"; switch (value) { case "test": type = "config-test.json"; break case "landdevelopment": type = "config-landdevelopment.json"; break case "utilitiesinfrastructure": type = "config-utilitiesinfrastructure.json"; break default: type = "config.json"; } return type; }, getFormattedServiceType: function(value) { var type = "Property Locator"; var newValue = (value.replace(".json", "")).replace('config-', ''); switch (newValue) { case "test": type = "Test"; break case "landdevelopment": type = "Land Development"; break case "utilitiesinfrastructure": type = "Utilities Infrastructure"; break default: type = "Property Locator"; } return type; }, getAllNamedChildDijits: function(id) { // Gather all child widgets var w = null; var children = null; widgets = {}; w = query("[widgetId]", dom.byId(id)); children = w.map(dijit.byNode); children.forEach(function(item, idx) { if (item.name) { widgets[item.name] = item; } }); return widgets; } });
appreciate that. I can load it from my index.html as long as i path to it correctly right?
also, what about defining the components properly so in the third function i can use "query" instead of "dojo.query"?
define(["dojo/dom", "dojo/query"], function(dom, query) { return { getType: function(value) { var type = "config.json"; switch (value) { case "test": type = "config-test.json"; break case "landdevelopment": type = "config-landdevelopment.json"; break case "utilitiesinfrastructure": type = "config-utilitiesinfrastructure.json"; break default: type = "config.json"; } return type; }, getFormattedServiceType: function(value) { var type = "Property Locator"; var newValue = (value.replace(".json", "")).replace('config-', ''); switch (newValue) { case "test": type = "Test"; break case "landdevelopment": type = "Land Development"; break case "utilitiesinfrastructure": type = "Utilities Infrastructure"; break default: type = "Property Locator"; } return type; }, getAllNamedChildDijits: function(id) { // Gather all child widgets var w = null; var children = null; widgets = {}; w = query("[widgetId]", dom.byId(id)); children = w.map(dijit.byNode); children.forEach(function(item, idx) { if (item.name) { widgets[item.name] = item; } }); return widgets; } } });
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=7, IE=9, IE=10"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no"> <title></title> <link rel="stylesheet" href="https://community.esri.com//js.arcgis.com/3.6/js/esri/css/esri.css"> <style> html, body, #map { height: 100%; width: 100%; margin: 0; padding: 0; } </style> <script> var dojoConfig = { paths: { utilities: location.pathname.replace(/\/[^/]+$/, "") } } </script> <script src="//js.arcgis.com/3.6/"></script> <script> var map; require([ "esri/map", // "utilities/common", "utilities/common-deps", "dojo/domReady!" ], function( Map, common ) { map = new Map("map", { basemap: "streets", center: [ 19.291, 48.343 ], zoom: 4 }); console.log("all of common", common); console.log("common.getType(\"test\"):", common.getType("test")); console.log("common.getAllNamedChildDijits(\"map\"):", common.getAllNamedChildDijits("map")); }); </script> </head> <body> <div id="map"></div> </body> </html>