I am trying to create a demo app to understand how to minimize my code by creating more files.
I am using the base starter app sample on one js file and trying to add a widget on another. The code works on the same file but after the divide its breaking.
I added a dojo config, a dojo-ready function and a new define in the widgets.js file.
I am using "define" and not "require" on the widget.js because it's dependent on the main but not sure if it's ok because it will only be called once.
This is how my app looks:
modules(folder)
-widgets.js
index.html
main.js
index.html
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<title>ArcGIS API for JavaScript Tutorials: Create a Starter App</title>
<style>
html, body, #viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</style>
<script type="text/javascript">
dojoConfig = {
async: true,
packages: [
{
name: "widgets",
location: "modules/widgets" // or wherever you keep your custom code
}
]
};
</script>
<link rel="stylesheet" href="https://js.arcgis.com/4.18/esri/themes/light/main.css">
<script src="https://js.arcgis.com/4.18/"></script>
<script type="text/javascript" src="main.js"></script>
</head>
<body>
<div id="viewDiv"></div>
</body>
</html>main.js
require([
"esri/Map",
"esri/views/MapView",
"dojo/ready",
"widgets/searchWidget"
], function(Map, MapView,ready) {
ready(function () {
var map = new Map({
basemap: "topo-vector"
});
var view = new MapView({
container: "viewDiv",
map: map,
center: [-118.80500, 34.02700], // longitude, latitude
zoom: 13
});
var widget = new searchWidget(view)
});
});widget.js
define(["esri/widgets/Search"], function(Search) {
var search
return{
searchWidget: function(view){
search = new Search({
view: view
});
view.ui.add(search, 'top-right');
}
}
});