Skip to content

Commit f8021c9

Browse files
committed
remove some TODOs, add errors
1 parent 533e222 commit f8021c9

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

js/autocomplete.js

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ var CSS = {
8181
ajaxError: 'ajax-error',
8282
ajaxLoading: 'ajax-loading',
8383
highlightedOption: 'highlighted',
84+
noResults: 'no-results',
8485
option: 'option',
8586
removeTokenGroup: 'remove-token-group',
8687
selectedTokenGroup: 'selected',
@@ -420,8 +421,8 @@ var validListObject = function(obj) {
420421
return false;
421422
}
422423

423-
// TODO: finish me
424424
// TODO: show an error when a value.children is not a valid list option
425+
// TODO: what else do we need to validate on the List Object?
425426

426427
return true;
427428
};
@@ -782,32 +783,51 @@ var buildWidget = function() {
782783
return html;
783784
};
784785

785-
// TODO: throw error if their optionHTML function did not return a string
786-
// TODO: need to revisit this; was very distracted when I last re-factored it
787786
var buildOptionHTML = function(option, parentList) {
787+
// optionHTML on the option object
788788
if (typeof option.optionHTML === 'string') {
789789
return option.optionHTML;
790790
}
791791

792+
// parent list has optionHTML as a string template
792793
if (typeof parentList.optionHTML === 'string' &&
793794
isObject(option.value) === true) {
794795
return tmpl(parentList.optionHTML, option.value, true);
795796
}
796797

798+
// parent list has optionHTML as a function
797799
if (typeof parentList.optionHTML === 'function') {
798-
return parentList.optionHTML(option);
800+
var html = parentList.optionHTML(option);
801+
if (typeof html === 'string') {
802+
return html;
803+
}
804+
805+
// throw error if their optionHTML function did not return a string
806+
error(3843, 'optionHTML function did not return a string.', option);
799807
}
800808

809+
// value is a string or number
801810
if (typeof option.value === 'string' ||
802811
typeof option.value === 'number') {
803812
return encode(option.value);
804813
}
805814

806-
// TODO: I should iterate through the .value here and
807-
// return the first thing that is a String
808-
// but also throw an error
809-
// TODO: list validation should ensure that this never
810-
// happens
815+
// TODO: list validation should ensure that we never get here
816+
817+
// last-ditch effort: iterate through option.value and print
818+
// the first thing that is a string
819+
if (isObject(option.value) === true) {
820+
for (var i in option.value) {
821+
if (option.hasOwnProperty(i) !== true) continue;
822+
if (typeof option[i] === 'string') {
823+
824+
error(3193, 'Could not find a valid value for optionHTML. ' +
825+
'Resorted to using value property "' + i + '"', option);
826+
827+
return encode(option[i]);
828+
}
829+
}
830+
}
811831

812832
// NOTE: this should never happen
813833
error(5783, 'Unable to create HTML string for optionHTML.', option);
@@ -877,7 +897,7 @@ var buildTokens = function(tokens) {
877897

878898
for (var j = 0; j < tokenGroup.length; j++) {
879899
html += '<span class="token">' +
880-
tokenGroup[j].tokenHTML + '</span>';
900+
tokenGroup[j].tokenHTML + '</span>';
881901

882902
// show child indicator
883903
if (j !== tokenGroup.length - 1) {
@@ -926,12 +946,11 @@ var buildStringOrFunction = function(cssClass, strOrFn, inputValue) {
926946
};
927947

928948
var buildNoResults = function(noResultsHTML, inputValue) {
929-
return buildStringOrFunction('no-results', noResultsHTML, inputValue);
949+
return buildStringOrFunction(CSS.noResults, noResultsHTML, inputValue);
930950
};
931951

932952
var buildLoading = function(ajaxLoadingHTML, inputValue) {
933-
return buildStringOrFunction(CSS.ajaxLoading,
934-
ajaxLoadingHTML, inputValue);
953+
return buildStringOrFunction(CSS.ajaxLoading, ajaxLoadingHTML, inputValue);
935954
};
936955

937956
//------------------------------------------------------------------------------

pages/docs.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,11 +726,21 @@
726726
"desc":"The first argument to the getList method must be a non-empty string.",
727727
"fix":"The first argument to <a href='docs#methods:getList'><code class='js plain'>getList</code></a> should be the name of a <a href='docs#list_object'>List Object</a> that is defined on <a href='docs#config_object:lists'><code class='js plain'>lists</code></a>."
728728
},
729+
{
730+
"id":3193,
731+
"desc":"Could not find a valid value for optionHTML. Resorted to using value property \"<propertyName>\"",
732+
"fix":"AutoComplete could not find a valid <code class='js plain'>optionHTML</code> on either the <a href='docs#option_object:optionHTML'>Option Object</a> or the <a href='docs#list_object:optionHTML'>List Object</a> so it resorted to using the first string <a href='docs#option_object:value'><code class='js plain'>option.value</code></a> property it could find."
733+
},
729734
{
730735
"id":3776,
731736
"desc":"Invalid Value returned from your custom onChange function.",
732737
"fix":"If you specify an <a href='docs#config_object:onChange'><code class='js plain'>onChange</code></a> function, it must return an array of <a href='docs#token_object'>Token Groups</a>."
733738
},
739+
{
740+
"id":3843,
741+
"desc":"optionHTML function did not return a string.",
742+
"fix":"If you provide a function for <a href='docs#list_object:optionHTML'><code class='js plain'>optionHTML</code></a> it must return an HTML string."
743+
},
734744
{
735745
"id":4823,
736746
"desc":"Error in removeTokenGroup method. Token group index \"<tokenGroupIndex>\" does not exist.",

0 commit comments

Comments
 (0)