Last active
October 19, 2015 17:22
-
-
Save odoe/626178b5a06fa31fb416 to your computer and use it in GitHub Desktop.
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
require([ | |
'dojo/on', | |
'esri/core/Collection', | |
'esri/tasks/QueryTask', | |
'esri/tasks/support/Query' | |
], function (on, Collection, QueryTask, Query) { | |
var url = 'http://services2.arcgis.com/j80Jz20at6Bi0thr/arcgis/rest/services/Parks/FeatureServer/0'; | |
var resultsNode = document.getElementById('resultsDiv'); | |
var input = document.getElementById('autoComplete'); | |
var qTask = new QueryTask({ | |
url: url | |
}); | |
var query = new Query({ returnGeometry: false, outFields: ['*'], where: '1=1' }); | |
var cache = new Collection(); | |
var collection = new Collection(cache); | |
cache.on('change', function () { | |
collection.clear(); | |
collection.addItems(cache); | |
}); | |
collection.on('change', function (e) { | |
resultsNode.innerHTML = ''; | |
collection.map(function (x) { | |
var node = document.createElement('li'); | |
node.setAttribute('class', 'list-group-item'); | |
node.innerText = x.NAME; | |
resultsNode.appendChild(node); | |
}); | |
}); | |
qTask.execute(query).then(function (results) { | |
var attrs = results.features.map(function (x) { return x.attributes; }); | |
// You could just do new Collection(attrs), but let's | |
// demonstrate some change events | |
cache.addItems(attrs); | |
}); | |
on(input, 'keyup', function (e) { | |
var val = input.value.toLowerCase(); | |
// filter the cache | |
var coll = cache.filter(function (x) { | |
return x.NAME.toLowerCase().indexOf(val) > -1; | |
}); | |
collection.clear(); | |
collection.addItems(coll); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment