Skip to content

Instantly share code, notes, and snippets.

@odoe
Last active October 19, 2015 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odoe/626178b5a06fa31fb416 to your computer and use it in GitHub Desktop.
Save odoe/626178b5a06fa31fb416 to your computer and use it in GitHub Desktop.
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