Skip to content

Instantly share code, notes, and snippets.

@raykendo
Last active December 8, 2015 23:17
Show Gist options
  • Save raykendo/6418dbc9750b22e2069c to your computer and use it in GitHub Desktop.
Save raykendo/6418dbc9750b22e2069c to your computer and use it in GitHub Desktop.
David Walsh's Display Unique Combobox (https://davidwalsh.name/unique-combobox) updated for AMD
/**
* An example using the UniqueComboBox defined above. This one creates a combobox with a datastore defined by
* dojo/store/Memory and dojo/store/Observable. The UniqueComboBoxMenu provides a way to remove duplicate values
* from the list of results returned.
*/
require([
"dojo/store/Memory",
"dojo/store/Observable",
"dijit/form/ComboBox",
"davidwalsh.form._UniqueComboBoxMenu",
"dojo/domReady!"
], function (Memory, Observable, ComboBox, _UniqueComboBoxMenu) {
var dataStore = new Observable(new Memory({
idProperty: "id",
data: []
}));
var combo = new ComboBox({
store: dataStore,
dropDownClass: _UniqueComboBoxMenu
}, "myNode");
});
/**
* An update on David Walsh's Unique Combobox
* Updated for Dojo AMD (tested at v. 1.10)
* Originally posted: https://davidwalsh.name/unique-combobox
*/
define("davidwalsh.form._UniqueComboBoxMenu",
["dojo/_base/declare", "dojo/_base/array", "dijit/form/_ComboBoxMenu"],
function (declare, arrayUtils, _ComboBoxMenu) {
return declare([_ComboBoxMenu], {
createOptions: function (results, dataObject, labelFunc) {
// Cycle through to find uniques
var uniqueKeys = {}, uniqueItems = [];
arrayUtils.forEach(results,function(result,index) {
var label = labelFunc(result);
if(typeof label != "string") {
label = label.label;
}
if(!uniqueKeys[label]) {
uniqueKeys[label] = result;
uniqueItems.push(result);
}
});
// Update arguments arr
arguments[0] = uniqueItems;
// Call inheritance chain for this method, return result
return this.inherited(arguments);
}
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment