On Github oregonGEO / DojoIntro
Beginning Developing with Esri Web AppBuilder
Created by OregonGEO
A JavaScript toolkit for building web apps.
It's just JavaScript!
// iterate through array with dojo
require(["dojo/_base/array"], function (array) {
var weather = ["rain", "sunshine", "fog", "hail", "thunderstorm"];
array.forEach(weather, function (item) {
console.log(item)
}, /* this */);
});
// iterate through array with jQuery
var weather = ["rain", "sunshine", "fog", "hail", "thunderstorm"];
$.each(weather, function (index, item) {
console.log(item);
});
// iterate through array with just JavaScript
var weather = ["rain", "sunshine", "fog", "hail", "thunderstorm"];
for(var i = 0; i < weather.length; i++) {
console.log(weather[i]);
}
// or modern support (ie >= 9.0)
weather.forEach(function (element, index, array) {
console.log(element);
});
function getWeather(city, cb) {
var cities = {
Salem : "62"
};
if(cities[city]) {
return cb(null, cities[city]);
} else {
return cb('no city by that name');
}
}
var weather = getWeather('Salem', function (err, temp) {
document.getElementById('output').innerHTML += 'Salem Temp: ' + ((err) ? err : temp);
});
var weather = getWeather('Portland', function (err, temp) {
if(err) {
// handle error
}
document.getElementById('output').innerHTML += '<br>Portland Temp: ' + ((err) ? err : temp);
});
The current context of you code (ie. what you have access to at any location in your code)
Can be local or global
var city = "Salem";
var changeCity = function(name) {
var city = name;
console.log(city);
};
console.log(city);
changeCity('Portland');
console.log(city);
'this' refers to current scope. By default, this refers to outer most global object 'window'.
console.log(this === window); // true
var Weather = function () {
var unit = 'metric';
this.city = 'Salem';
this.getCity = function () {
console.log(this.city);
};
};
var report = new Weather();
console.log(report.city);
report.getCity();
report.getCity.call({city : 'Portland'});
jsFiddle
Async operations are non blocking, meaning your code will not wait for it to finish.
// jQuery AJAX Example
$.ajax({
url : 'someUrl',
method : 'GET',
dataType : 'JSON',
success : function (response) {
console.log('Here is your data');
}
});
console.log('Done getting data');
define(function () {
var sayWeather = function () {
alert('Here is your weather report');
};
return {
sayWeather : sayWeather
}
});
See Example
Group like code and functionality into modules
// app/counter.js
define(function(){
var privateValue = 0;
return {
increment: function(){
privateValue++;
},
decrement: function(){
privateValue--;
},
getValue: function(){
return privateValue;
}
};
});
require(['app/counter'], function (counter) {
counter.increment();
counter.increment();
counter.getValue(); // returns 2
});
define([
"dojo/_base/declare",
"dojo/dom",
"app/dateFormatter"
], function(declare, dom, dateFormatter){
return declare(null, {
showDate: function(id, date){
dom.byId(id).innerHTML = dateFormatter.format(date);
}
});
});
Classes in Dojo with dojo/_base/declare
Widgets in Esri Web AppBuilder follow this design pattern