最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using Knockout with Typeahead.js and Bloodhound.js v0.10 - Stack Overflow

programmeradmin6浏览0评论

I have just converted over to using Bloodhound.js and Typeahead.js with Knockout. I am having a few issues -

  1. The Typeahead is not displaying the Name property in the suggestions list
  2. I can't figure out how to update the set to query against

/

I am 'statically' adding new data right now but it should still show up in the result set, and is not

self.addNew = function () {
    self.someOptions.push(new Option(self.someOptions().length + 1, 'Johnnn'));
}

I am up for any suggestions that work, so I am not stuck using Bloodhound.js if it isn't required, but due to the nature of my app I cannot remove the properties as observables, but calling ko.toJS at some point is definitely an option.

I have just converted over to using Bloodhound.js and Typeahead.js with Knockout. I am having a few issues -

  1. The Typeahead is not displaying the Name property in the suggestions list
  2. I can't figure out how to update the set to query against

http://jsfiddle/Ea93f/2/

I am 'statically' adding new data right now but it should still show up in the result set, and is not

self.addNew = function () {
    self.someOptions.push(new Option(self.someOptions().length + 1, 'Johnnn'));
}

I am up for any suggestions that work, so I am not stuck using Bloodhound.js if it isn't required, but due to the nature of my app I cannot remove the properties as observables, but calling ko.toJS at some point is definitely an option.

Share Improve this question asked Feb 26, 2014 at 23:28 PW KadPW Kad 15k7 gold badges51 silver badges82 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

The first issue is with the displayKey. You need to supply an explicit function in your binding

HTML

<input type="text" data-bind="typeahead: { name: 'something', taOptions: theseOptions, displayKey: 'Name' }, value: thisValue" />

Javascript

    // In ko.bindingHandlers.typeahead.init function
    var displayKey = options.displayKey;
    options.displayKey = function(item) {
        return item[displayKey]();
    };

The second problem is with the use of local. It looks like the system doesn't recalculate the source after initialization. Looking at the docs you probably need to make use of the remote option and pretend to be an ajax request/response. You also need to implement your own result filter, as well as hacking around either requestCache when options are updated.

I've update your jsFiddle with the following...

self.theseOptions = new Bloodhound({
  datumTokenizer: function(d) { 
      var seomth = Bloodhound.tokenizers.whitespace(d.Name());
      console.log(seomth);
      return seomth },
  queryTokenizer: Bloodhound.tokenizers.whitespace,
    remote : {
        url : '%QUERY',
        transport : function(url, options, onSuccess, onError) {
            var deferred = $.Deferred();
            deferred.done( function() { onSuccess(this); });

            var filterVal = url.toLowerCase();
            var result = self.someOptions().filter( function(item) {
                return !!~item.Name().toLowerCase().indexOf(filterVal);
            });
            deferred.resolveWith( result );
            return deferred.promise();
        }

    }
  //local: self.someOptions()
});

self.addNew = function () {
    self.someOptions.push(new Option(self.someOptions().length + 1, 'Johnnn'));
    self.theseOptions.transport.constructor.resetCache();
}
发布评论

评论列表(0)

  1. 暂无评论