algoliasearch-client-javascript icon indicating copy to clipboard operation
algoliasearch-client-javascript copied to clipboard

Typeahead attributeName to obj.value mapping

Open andy3rdworld opened this issue 13 years ago • 1 comments

instead of setting the value of obj (obj.value) equal to the first match, would it make more sense to allow an argument in the constructor to explicitly the set the mapping between the propertyName and the object.value so that the order in which the object attributes are created in the index does not determine how the object.value <--> attributeName mapping is determined?

// Method used by Typeahead.js.
        get: function(query, processRemoteData, that, cb, suggestions) {
            self = this;
            this.search(query, function(success, content) {
                if (success) {
                  for (var i = 0; i < content.hits.length; ++i) {
                    // Add an attribute value with the first string
                    var obj = content.hits[i];
                    var found = false;
                    if (typeof obj.value === 'undefined') {
                      for (var propertyName in obj) {
                        if (!found && obj.hasOwnProperty(propertyName) && typeof obj[propertyName] === 'string') {
                          obj.value = obj[propertyName];
                          found = true;
                        }
                      }
                    }
                    suggestions.push(that._transformDatum(obj));
                  }
                  cb && cb(suggestions);
                }
              }, self.typeAheadArgs);
            return true;
        },

andy3rdworld avatar Nov 02 '13 21:11 andy3rdworld

Yes you are right. I have just added a new parameter in getTypeaheadTransport function to specify the propertyName to use.

speedblue avatar Nov 03 '13 09:11 speedblue