Value converter issue with repeat.for
Explanation
I am having an issue when trying to use a value converter with a repeat.for where the values do not show up (it also seems to empty my array). When I exclude the value converter, the values show up as expected. My value converter seems to be coded fine, and I cannot figure out why this is happening. (I am using TypeScript)
Code
Value Converter
Simple value converter to order a string array by length descending
export class StringLengthValueConverter {
toView(array: Array<string>) {
return array.splice(0)
.sort((x, y) => {
return x.length === y.length
? 0
: x.length > y.length ? -1 : 1;
});
}
}
View
Show the people in a list
<ul class="list-group">
<li class="list-group-item" repeat.for="person of people | stringLength">
<span>${person}</span>
</li>
</ul>
View Model
Populates the people array
export class Welcome {
people: Array<string> = ["Chris", "JohnnyBoy", "Al"];
}
Question
Am I am doing something wrong in the value converter? Is this a bug? Any help would be appreciated. Thank you.
Here is a sorting valueConverter (alphanumeric sort) as an example since I'm having trouble parsing your sorting logic - but generally these questions are best asked on the Gitter channel: https://gitter.im/Aurelia/Discuss
Also - here are a bunch of example valueConverters - if yours is missing feel free to add it once you've worked the bugs out: https://github.com/Vheissu/Aurelia-Code-Snippets/tree/master/Value%20Converters
/**
* Sort array value converter
*
* This value converter takes an array with a config parameter and sorts the array and
* returns it to the view.
*
* Usage:
* <require from="sort-array"></require>
* <pre>${object | sort:{ property: 'doc_count', direction: 'desc' } }</pre>
*/
export class SortValueConverter {
/**
* To View method
*
* @param {array} array to sort
* @param {Object} config sort configuration
*
* @return {array} sorted array
* @example
* "item of $parent.results.facets[facet.name] | sort: { property: 'doc_count', direction: 'desc' }"
*
*/
toView(array, config) {
return array
.sort((val1, val2) => {
var a = val1, b = val2;
if (config.direction.toLowerCase() !== 'asc' && config.direction.toLowerCase() !== 'ascending') {
a = val2;
b = val1;
}
return a[config.property] > b[config.property];
});
}
}
I have looked at these example already. All my value converter is doing is sorting by the length of the string.
It actually looks like the issue was with an h3 tag I had about the list that show the length of the people array. Also, if I try to add another item to the people array then the array empties and only the newly added item remains. Removing that fixes the issue for some reason? Anyone have an idea why?
@cknightdevelopment did you mean to use slice here: return array.splice(0)...
ARRRGGGHHHH!
Thanks @jdanyow. One friggin' letter! Close it ... just close it.
hahahaha- it happens... especially on Fridays
@cknightdevelopment Trust me, we've all made that mistake. :smile: