Customizing easyAutoComplete

413 Views Asked by At
 var options = {
        url: function (phrase) {
            return "location/autoComplete?key=" + phrase;
        },

        getValue: "cityName",

        template: {
            type: "custom",
            method: function (value, item) {
                return value + ",  " + item.stateName;
            }
        },

        list: {

            onClickEvent: function () {
                var selectedLongitude = $("#autoCompleteSearch").getSelectedItemData().longitude;
                var selectedLatitude = $("#autoCompleteSearch").getSelectedItemData().latitude;


                userPos = {
                    lat: Number(selectedLatitude),
                    lng: Number(selectedLongitude)
                };
                map.setCenter(userPos);
                map.setZoom(17)
            },

            showAnimation: {
                type: "fade", //normal|slide|fade
                time: 400,
                callback: function () {
                }
            },

            hideAnimation: {
                type: "slide", //normal|slide|fade
                time: 400,
                callback: function () {
                }
            }
        }
    };

This is the code I am using right now to fetch a remote webservice data. Now I need to get the data and remove the duplicate "city names". Also I need to show "Data not available" or some custom method when there is no data from the webservice. I am kind of stuck. Would be great if someone could point me on how to achieve this.

1

There are 1 best solutions below

0
Lajos Arpad On

You can get rid of duplicates using a helper function:

function removeDuplicates(input) {
    if (!input) { //Falsy input
        return input;
    }
    var isArray = Array.isArray(input) ? [] : {};
    var items = [];
    var output = {};
    for (var key in input) {
        if (items.indexOf(input[key]) < 0) {
            items.push(input[key]);
            if (!isArray) {
                output[key] = input[key];
            }
        } 
    }
    return isArray ? items : output;
}

You can show your custom text if Array.isArray(input) ? (input.length === 0) : (Object.keys(input).length === 0 && input.constructor === Object).