【问题标题】:Cannot update twitter typeahead.js prefetch thumbprint after initial load初始加载后无法更新 twitter typeahead.js 预取指纹
【发布时间】:2014-02-19 20:11:33
【问题描述】:

我在应用程序中使用 typeahead (0.10.1) 来返回使用 Bloodhound 预取的客户端列表。我不知道如何使用新的指纹更新/重新初始化寻血犬,以便在插入新客户端后它将使用新的指纹并因此包含新客户端。

系统是 ajax,所以我不能只在刷新时更改名称,我试图在 reinitializeclient() 中按照下面的方法向它传递另一个指纹,但它不起作用:

var clientthumbprint = "initialname";
var clients = new Bloodhound({ 
    limit: 5,
    prefetch:{
    url: '/urltofeed/',
    thumbprint:getthumbprint()
   },
  datumTokenizer: function(d) { 
  return Bloodhound.tokenizers.whitespace(d.label); 
},
  queryTokenizer: Bloodhound.tokenizers.whitespace
});
clients.initialize();

function reinitializeclient(newthumbprint){
  //This is called when a client is saved
  clientthumbprint = newthumprint;
  clients.initialize(); //this didn't work
}  

function getthumbprint(){
  return clientthumbprint;
}

$('#search_user').typeahead({
    minLength: 2,
  },
  {
   displayKey: 'label',
   source: clients.ttAdapter()
})

有人知道我做错了什么吗?

编辑:使用@jharding 提到的拉取请求更新我的预输入文件后,我可以重置它,但不能重新初始化。使用以下内容:

function initialize_clients(){
  clients = new Bloodhound({ 
    limit: 5,
    prefetch:{
      url: '/pathtojson/',
      thumbprint:getthumbprint()
    },
    datumTokenizer: function(d) { 
      return Bloodhound.tokenizers.whitespace(d.label); 
    },
    queryTokenizer: Bloodhound.tokenizers.whitespace
  });
  clients.initialize();
};  

function resetclients(){    
  clientthumbprint = 'somenewthumbprint';
  clients.reset();//this works - after running nothing will return in search
  initialize_clients();//this is not working
}  

function getthumbprint(){
   return clientthumbprint;
}

【问题讨论】:

  • 您现在无法重新初始化 Bloodhounds,但该功能将在 v0.10.2 中存在。这是相关的拉取请求:github.com/twitter/typeahead.js/pull/703
  • 太棒了!我抓住了那个拉取请求,并用 grunt 作为测试更新了我的文件。我可以重置它,但我似乎无法重新初始化它:
  • 尝试在重新初始化时将true 传递给initialize 方法。我仍在研究我想要的 API,如果您有任何想法,您应该将它们发布在拉取请求中。
  • @jharding 如果我通过尝试,它会重新初始化,但它仍在使用旧数据。 clients.initialize(true); - 有任何想法吗。将我的想法发布到拉取请求中。本质上,我希望能够再次指定要预取的来源。

标签: javascript twitter-bootstrap-3 typeahead.js bloodhound


【解决方案1】:

您可能已经对此有了答案,但我想我会添加对我有用的代码

//This seems to refresh my prefetch data
clients.clearPrefetchCache();

//not sure whether setting to true does anything 
//though, but according to the bloodhound.js it should force a reinitialise
clients.initialize(true);

希望对你有帮助

【讨论】:

    【解决方案2】:

    在我的例子中,我使用预取从 url 作为 JSON 数据得到了我的结果, 并且只想清除预取数据(因为我需要在添加一些其他数据后清除它)

    clients.initialize(true); 方法对我有用

    【讨论】:

      【解决方案3】:

      这对我来说总是可以清除本地/预取/远程数据并重新加载它:

      clients.clear();
      clients.clearPrefetchCache();
      clients.clearRemoteCache();
      clients.initialize(true);
      

      【讨论】:

      • 谢谢。这个项目似乎已经被放弃了——你知道有谁选择了它进行积极的开发吗?
      • 不,我不知道是否有人收到了它,但我花了几天时间处理这里描述的问题,现在重要的是我解决了问题,哈哈跨度>
      猜你喜欢
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-21
      • 2014-05-09
      • 2022-08-23
      • 1970-01-01
      相关资源
      最近更新 更多