Last active
August 29, 2015 14:11
-
-
Save noelweichbrodtVSolvit/124e5ad2fb51c3215d64 to your computer and use it in GitHub Desktop.
HUD CDPM Callbacks -> Promises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- // Call this to kick off a search for Grantees for the currently selected | |
- // state. | |
- function _doGranteeSearchesByState() { | |
- // ****************************************************************** | |
- // ****************************************************************** | |
- // Since we are calling BOTH type of searches now, we do not want to | |
- // reset these here. But if we change things back to do one or the | |
- // other we need to deal with these!!!! See _doSearch instead. | |
- // ****************************************************************** | |
- // ****************************************************************** | |
- //iGranteeQueryPendingCount = 0; | |
- //iGranteeQueryCompletedCount = 0; | |
- //iGranteeQueryErrorCount = 0; | |
- // ****************************************************************** | |
- | |
- if (includeCdbgGrantees) { | |
- iGranteeQueryPendingCount++; | |
- _doGranteeSearchByState(cdbgGranteeQueryTask,0); | |
- } | |
- if (includeEsgGrantees) { | |
- iGranteeQueryPendingCount++; | |
- _doGranteeSearchByState(esgGranteeQueryTask,1); | |
- } | |
- if (includeHomeGrantees) { | |
- iGranteeQueryPendingCount++; | |
- _doGranteeSearchByState(homeGranteeQueryTask,2); | |
- } | |
- if (includeHopwaGrantees) { | |
- iGranteeQueryPendingCount++; | |
- _doGranteeSearchByState(hopwaGranteeQueryTask,3); | |
- } | |
- if (includeCocGrantees) { | |
- iGranteeQueryPendingCount++; | |
- _doGranteeSearchByState(cocGranteeQueryTask,4); | |
- } | |
- if (includeDrgrGrantees) { | |
- iGranteeQueryPendingCount++; | |
- _doGranteeSearchByState(drgrGranteeQueryTask, 7); | |
- } | |
- // For search by state, we omit UGLG since it would return too many. | |
- //if (includeUglgGrantees) { | |
- // iGranteeQueryPendingCount++; | |
- // _doGranteeSearchByState(uglgGranteeQueryTask, 6); | |
- //} | |
- | |
- // note: see _onGranteeSearchSuccess | |
+ function OnGranteeSearchFullfillment(results) { | |
+ if (results.cdbg != null) ProcessSearchResults(results.cdbg, SearchType.cdbg, true); | |
+ if (results.cdbgState != null) ProcessSearchResults(results.cdbgState, SearchType.cdbg, false); | |
+ if (results.esg != null) ProcessSearchResults(results.esg, SearchType.esg, true); | |
+ if (results.esgState != null) ProcessSearchResults(results.esgState, SearchType.esg, false); | |
+ if (results.home != null) ProcessSearchResults(results.home, SearchType.home, true); | |
+ if (results.homeState != null) ProcessSearchResults(results.homeState, SearchType.home, false); | |
+ if (results.hopwa != null) ProcessSearchResults(results.hopwa, SearchType.hopwa, true); | |
+ if (results.hopwaState != null) ProcessSearchResults(results.hopwaState, SearchType.hopwa, false); | |
+ if (results.coc != null) ProcessSearchResults(results.coc, SearchType.coc, true); | |
+ if (results.cocState != null) ProcessSearchResults(results.cocState, SearchType.coc, false); | |
+ if (results.uglg != null) ProcessSearchResults(results.uglg, SearchType.uglg, true); | |
+ if (results.uglgState != null) ProcessSearchResults(results.uglgState, SearchType.uglg, false); | |
+ if (results.drgr != null) ProcessSearchResults(results.drgr, SearchType.drgr, true); | |
+ if (results.drgrState != null) ProcessSearchResults(results.drgrState, SearchType.drgr, false); | |
} | |
- // Helper function to kick off a search for Grantees specific to the type | |
- // associated with the QueryTask passed in - for the current state. | |
- function _doGranteeSearchByState(qTask, layerId) { | |
- if (qTask == null) | |
- return; | |
- | |
- // init the query... | |
- var query = new Query(); | |
- query.where = "STUSAB="; | |
- query.where += "'" + currentState + "'"; | |
- query.spatialRelationship = Query.SPATIAL_REL_INTERSECTS; | |
- query.returnGeometry = false; | |
- query.outFields = ["OBJECTID", "NAME"]; | |
- | |
- // tell it which "onSuccess" function to call if the query succeeds. | |
- if (layerId == 0) | |
- qTask.execute(query, _onCdbgGranteeSearchSuccess, _onGranteeSearchFault); | |
- else if (layerId == 1) | |
- qTask.execute(query, _onEsgGranteeSearchSuccess, _onGranteeSearchFault); | |
- else if (layerId == 2) | |
- qTask.execute(query, _onHomeGranteeSearchSuccess, _onGranteeSearchFault); | |
- else if (layerId == 3) | |
- qTask.execute(query, _onHopwaGranteeSearchSuccess, _onGranteeSearchFault); | |
- else if (layerId == 4) { | |
- query.outFields.push("COCNAME"); | |
- qTask.execute(query, _onCocGranteeSearchSuccess, _onGranteeSearchFault); | |
- } | |
- else if (layerId == 6) { | |
- query.outFields.push("UGLGNAME"); | |
- qTask.execute(query, _onUglgGranteeSearchSuccess, _onGranteeSearchFault); | |
- } | |
- else if (layerId == 7) { | |
- query.outFields = ["OBJECTID", "KEYFIELD", "GRANTEE_NAME"]; | |
- qTask.execute(query, _onDrgrGranteeSearchSuccess, _onGranteeSearchFault); | |
- } | |
- } | |
- | |
- // These are called by the specific types of grantee QueryTask if it succeeded. | |
- // These forward things to our generic handler. | |
- function _onCdbgGranteeSearchSuccess(results) { | |
- _onGranteeSearchSuccess(results, "CDBG", true); | |
- } | |
- | |
- function _onEsgGranteeSearchSuccess(results) { | |
- _onGranteeSearchSuccess(results, "ESG", true); | |
- } | |
- | |
- function _onHomeGranteeSearchSuccess(results) { | |
- _onGranteeSearchSuccess(results, "HOME", true); | |
- } | |
- | |
- function _onHopwaGranteeSearchSuccess(results) { | |
- _onGranteeSearchSuccess(results, "HOPWA", true); | |
- } | |
- | |
- function _onCocGranteeSearchSuccess(results) { | |
- _onGranteeSearchSuccess(results, "COC", true); | |
- } | |
- function _onUglgGranteeSearchSuccess(results) { | |
- _onGranteeSearchSuccess(results, "UGLG", true); | |
- } | |
- | |
- function _onDrgrGranteeSearchSuccess(results) { | |
- _onGranteeSearchSuccess(results, "CDBG-DR", true); | |
- } | |
- | |
- function _onCdbgGranteeSearchAddressSuccess(results) { | |
- _onGranteeSearchSuccess(results, "CDBG", false); | |
- } | |
- | |
- function _onEsgGranteeSearchAddressSuccess(results) { | |
- _onGranteeSearchSuccess(results, "ESG", false); | |
- } | |
- | |
- function _onHomeGranteeSearchAddressSuccess(results) { | |
- _onGranteeSearchSuccess(results, "HOME", false); | |
+ function OnGranteeSearchError(error) { | |
+ alert(_sErrorGettingGranteeInfo); | |
+ console.log("OnGranteeSearchError: " + error.toString()); | |
} | |
- function _onHopwaGranteeSearchAddressSuccess(results) { | |
- _onGranteeSearchSuccess(results, "HOPWA", false); | |
- } | |
- | |
- function _onCocGranteeSearchAddressSuccess(results) { | |
- _onGranteeSearchSuccess(results, "COC", false); | |
- } | |
- | |
- function _onUglgGranteeSearchAddressSuccess(results) { | |
- _onGranteeSearchSuccess(results, "UGLG", false); | |
- } | |
- | |
- function _onDrgrGranteeSearchAddressSuccess(results) { | |
- _onGranteeSearchSuccess(results, "CDBG-DR", false); | |
+ function OnAddressSearchError(error) { | |
+ alert(_sErrorInAddressResults); | |
+ console.log("OnGranteeSearchError: " + error.toString()); | |
} | |
// Validates the searchField, then kicks off asynchronous searches by state, | |
// and address (the results will be unioned together eventually). | |
- function _doSearch() { | |
- _clearSearchResults(); | |
- | |
- searchText = $("#searchField").val(); | |
- searchText = _trim(searchText); | |
- currentState = ""; // will be set below as needed. | |
- currentAddressCandidate = null; | |
- currentFilter = null; | |
- | |
- if (searchText == null || searchText == "") { | |
- alert(_sEnterSearch); | |
- return; | |
- } | |
- | |
- var words = searchText.split(" "); | |
- if (words == null || words.length==0) { | |
- alert(_sEnterSearch); | |
- return; | |
- } | |
- | |
- // Note: order here is important! | |
- | |
- // Here are the types of searches we support. Note that statename can be 1, 2, | |
- // or 3 words (e.g. 'alabama', 'new york', , 'District of Columbia'), but will | |
- // get mapped to currentState using the appropriate state abbreviation. | |
+ function _doSearch(searchTerm) { | |
+ // Here are the types of searches we support. | |
// address | stateabbr | statename | keyword stateabbr | keyword statename | |
// Note, a full address is any search string which includes a word which | |
// starts with a number (e.g. box or house number, or zip, etc.). | |
- var isAddress = false; | |
- var i = 0; | |
- while (i < words.length && !isAddress) { | |
- if (_startsWithDigit(words[i])) { | |
- isAddress = true; | |
- _isSearchingOnJustAState = false; | |
- } | |
- i++; | |
- } | |
- if (!isAddress) { | |
- // This helps prevent searching on UGLG for an entire state later on, and sets | |
- // the value of the _isXWordStateName variables. | |
- if (_containsAnythingButAState(searchText)) | |
- _isSearchingOnJustAState = false; | |
- | |
- if (words.length == 1) { | |
- currentState = _getStateCode(words[0]); | |
- | |
- // The only valid searches with 1 word are a state name with one | |
- // word (e.g. 'alabama'), or state abbreviation. | |
- if (!_isSearchingOnJustAState) { | |
- alert(_sInvalidStateSearch); | |
- return; | |
- } | |
- } | |
- else { | |
- // if they entered exactly two words, either both words together must give a non-abbreviated | |
- // state (e.g. "new york"), or the second word must be either a state abbreviation (e.g. | |
- // 'baltimore MD'), or a "one word" state name (e.g. 'baltimore maryland') - in which case | |
- // we'll treat the first word as the currentFilter (e.g. baltimore will be the currentFilter | |
- // in the example given). | |
- | |
- // if they entered exactly three words, either all words together must give a non-abbreviated | |
- // state (e.g. "district of columbia"), or the second and third word must be a two word | |
- // state (e.g. 'durham north carolina'), and we'll use the first word as currentFilter | |
- // (e.g. durham will be used as the currentFilter in the preceding case). | |
- | |
- // if they entered exactly 4 words, then the second and third and fourth word must be a | |
- // three word state (e.g. 'cdbg district of columbia'), and we'll treat the first word as | |
- // the currentFilter (e.g. cdbg will be the currentFilter in the preceding case). | |
- | |
- // Note that the containsState functions will set currentState and currentFilter (if needed). | |
- if (!_isSearchingOnJustAState && !_containsStateAtEnd(searchText) && !_containsStateAbbrAtEnd(searchText)) { | |
- alert(_sInvalidTermAndStateSearch); | |
- return; | |
- } | |
- } | |
- } | |
+ // Note that statename can be 1, 2, | |
+ // or 3 words (e.g. 'alabama', 'new york', , 'District of Columbia'), but will | |
+ // get mapped to currentState using the appropriate state abbreviation. | |
+ // The only valid searches with 1 word are a state name with one | |
+ // word (e.g. 'alabama'), or state abbreviation. | |
+ // if they entered exactly two words, either both words together must give a non-abbreviated | |
+ // state (e.g. "new york"), or the second word must be either a state abbreviation (e.g. | |
+ // 'baltimore MD'), or a "one word" state name (e.g. 'baltimore maryland') - in which case | |
+ // we'll treat the first word as the currentFilter (e.g. baltimore will be the currentFilter | |
+ // in the example given). | |
+ | |
+ // if they entered exactly three words, either all words together must give a non-abbreviated | |
+ // state (e.g. "district of columbia"), or the second and third word must be a two word | |
+ // state (e.g. 'durham north carolina'), and we'll use the first word as currentFilter | |
+ // (e.g. durham will be used as the currentFilter in the preceding case). | |
+ | |
+ // if they entered exactly 4 words, then the second and third and fourth word must be a | |
+ // three word state (e.g. 'cdbg district of columbia'), and we'll treat the first word as | |
+ // the currentFilter (e.g. cdbg will be the currentFilter in the preceding case). | |
// For this version, we are doing a NON-UGLG search by state, and | |
// then an address lookup, then geospatial intersects on the geometry | |
@@ -895,109 +509,179 @@ | |
// entered more than a state. | |
// ****************************************************************** | |
- // ****************************************************************** | |
- // Since we are calling BOTH type of searches now, we need to reset | |
- // these here instead of within each function below. If we change | |
- // things back to do one or the other type of search, but not both, | |
- // then we need to deal with these!!!! | |
- // ****************************************************************** | |
- // ****************************************************************** | |
- // Initializing this to 1 prevents finalization before we've done | |
- // the search by address (which will deal with the extra 1 later). | |
- iGranteeQueryPendingCount = 1; | |
- iGranteeQueryCompletedCount = 0; | |
- iGranteeQueryErrorCount = 0; | |
- // ****************************************************************** | |
- | |
- showWaitCursor(true); | |
- | |
// NOTE: Order here is important - search by state should be first, | |
// since it resets some variables used to control flow (i.e. so we | |
// know when we are done, etc.). | |
- _doGranteeSearchesByState(); | |
- _doAddressLookup(); | |
- + //https://developers.arcgis.com/javascript/jssamples/query_deferred_list.html | |
+ | |
+ var promises = { // named promises so we can match up with the right layer name and metadata when fulfilled. We'll fill or not, and check when fulfilled | |
+ cdbg: null, | |
+ cdbgState: null, | |
+ esg: null, | |
+ esgState: null, | |
+ home: null, | |
+ homeState: null, | |
+ hopwa: null, | |
+ hopwaState: null, | |
+ coc: null, | |
+ cocState: null, | |
+ uglg: null, | |
+ uglgState: null, | |
+ drgr: null, | |
+ drgrState: null | |
+ }; | |
+ | |
+ // kick off search by address | |
+ var esriLocatorOptions = { | |
+ address: { "SingleLine": searchTerm.text }, | |
+ outFields: ["Loc_name"] | |
+ }; | |
+ new Locator("http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer").addressToLocations(esriLocatorOptions).then(function (event) { | |
+ // Helper function to kick off a search for Grantees whose geometry intersects | |
+ // the address the user entered. Pass the QueryTask and layerId associated with | |
+ // the type of grantee being searched for (e.g. pass cdbgGranteeQueryTask for | |
+ // qt, and 0 for layerId since CDBG is layer 0 in the HUD grantee web server). | |
+ // It will eventually take the user to the Choose Grantee widget. | |
+ // kick off a search for Grantees using the geocode item ESRI | |
+ // returned to us for the address the user has entered. It will eventually | |
+ // take the user to the Choose Grantee Widget. | |
+ if (event == null || event.length < 1 || event[0].address == null || event[0].location == null) | |
+ return; //finalize | |
+ var query = new Query(); | |
+ query.geometry = event[0].location; //TODO: should this deal with multiple location matches, eg Joplin MO (city) and Joplin Regional Airport (grantee)? | |
+ query.spatialRelationship = Query.SPATIAL_REL_INTERSECTS; | |
+ query.returnGeometry = false; | |
+ query.outFields = SearchType.cdbg.searchfields; | |
+ //query.outSpatialReference = map.spatialReference; | |
+ if (includeCdbgGrantees) promises.cdbg = new QueryTask(granteeURL + "/" + SearchType.cdbg.layer).execute(query); | |
+ if (includeEsgGrantees) promises.esg = new QueryTask(granteeURL + "/" + SearchType.esg.layer).execute(query); | |
+ if (includeHomeGrantees) promises.home = new QueryTask(granteeURL + "/" + SearchType.home.layer).execute(query); | |
+ if (includeHopwaGrantees) promises.hopwa = new QueryTask(granteeURL + "/" + SearchType.hopwa.layer).execute(query); | |
+ if (includeCocGrantees) promises.coc = new QueryTask(granteeURL + "/" + SearchType.coc.layer).execute(query); | |
+ if (includeUglgGrantees) promises.uglg = new QueryTask(granteeURL + "/" + SearchType.uglg.layer).execute(query); | |
+ if (includeDrgrGrantees) { | |
+ query.outFields = SearchType.drgr.searchfields; | |
+ promises.drgr = new QueryTask(granteeURL + "/" + SearchType.drgr.layer).execute(query); | |
+ } | |
+ }, OnAddressSearchError).always(function () { // wait until the address-level succeeds or fails, but always do the state-level search. | |
+ // kick off a search for Grantees for the current state. | |
+ var query = new Query(); | |
+ query.where = "STUSAB='" + searchTerm.stateCode.toUpperCase() + "'"; | |
+ query.spatialRelationship = Query.SPATIAL_REL_INTERSECTS; | |
+ query.returnGeometry = false; | |
+ if (includeCdbgGrantees) { | |
+ query.outFields = SearchType.cdbg.searchfields; | |
+ promises.cdbgState = new QueryTask(granteeURL + "/" + SearchType.cdbg.layer).execute(query); | |
+ } | |
+ if (includeEsgGrantees) { | |
+ query.outFields = SearchType.esg.searchfields; | |
+ promises.esgState = new QueryTask(granteeURL + "/" + SearchType.esg.layer).execute(query); | |
+ } | |
+ if (includeHomeGrantees) { | |
+ query.outFields = SearchType.home.searchfields; | |
+ promises.homeState = new QueryTask(granteeURL + "/" + SearchType.home.layer).execute(query); | |
+ } | |
+ if (includeHopwaGrantees) { | |
+ query.outFields = SearchType.hopwa.searchfields; | |
+ promises.hopwaState = new QueryTask(granteeURL + "/" + SearchType.hopwa.layer).execute(query); | |
+ } | |
+ if (includeCocGrantees) { | |
+ query.outFields = SearchType.coc.searchfields; | |
+ promises.cocState = new QueryTask(granteeURL + "/" + SearchType.coc.layer).execute(query); | |
+ } | |
+ // For search by state, we omit UGLG since it would return too many. | |
+ if (includeDrgrGrantees) { | |
+ query.outFields = SearchType.drgr.searchfields; | |
+ promises.drgrState = QueryTask(granteeURL + "/" + SearchType.drgr.layer).execute(query); | |
} | |
+ //pull all the promises together, execute, deal with success or failure, and finalize | |
+ all(promises).then(OnGranteeSearchFullfillment, OnGranteeSearchError).always(_finalizeGranteeSearch); | |
+ }); | |
+ }d |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment