admin管理员组

文章数量:1315329

I'm using typeahead (0.10.1) in an app to return a list of clients using bloodhound prefetch. I can't work out how to update/reinitialise bloodhound with a new thumbprint so that after the new client is inserted it will use a new thumbprint and hence include the new client.

The system is ajax so I can't just change the name on refresh, I'm trying to pass it another thumbprint as per below in the reinitializeclient() but it's not working:

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()
})

Anyone have any ideas what I'm doing wrong?

EDIT: After updating my typeahead files using the pull request mentioned by @jharding, I can get it to reset, but not reinitialise.Using the following:

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;
}

I'm using typeahead (0.10.1) in an app to return a list of clients using bloodhound prefetch. I can't work out how to update/reinitialise bloodhound with a new thumbprint so that after the new client is inserted it will use a new thumbprint and hence include the new client.

The system is ajax so I can't just change the name on refresh, I'm trying to pass it another thumbprint as per below in the reinitializeclient() but it's not working:

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()
})

Anyone have any ideas what I'm doing wrong?

EDIT: After updating my typeahead files using the pull request mentioned by @jharding, I can get it to reset, but not reinitialise.Using the following:

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;
}
Share Improve this question edited Feb 20, 2014 at 15:39 MrTomTom asked Feb 19, 2014 at 20:11 MrTomTomMrTomTom 3374 silver badges12 bronze badges 4
  • You can't reinitialize Bloodhounds right now, but that ability will exist in v0.10.2. Here's the related pull request: github./twitter/typeahead.js/pull/703 – jharding Commented Feb 20, 2014 at 7:06
  • Great! I grabbed that pull request and updated my files with grunt as a test. I can get it to reset, but I don't seem to be able to reinitialise it: – MrTomTom Commented Feb 20, 2014 at 15:32
  • Try passing true to the initialize method when reinitializing. I'm still working through what I want the API to be, if you have any thoughts, you should post them on the pull request. – jharding Commented Feb 20, 2014 at 22:31
  • @jharding It reinitialises if I pass through try, but it is still using the old data. clients.initialize(true); -- Any ideas. Will post my thoughts to the pull request. essentially I'd love to be able to also specify which sources to prefetch again. – MrTomTom Commented Feb 21, 2014 at 1:36
Add a ment  | 

3 Answers 3

Reset to default 8

You probably had an answer to this already but i thought i would add the code that worked for me

//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);

Hope it helps

In my case, I got my results from url as JSON data using prefetch, and wanted to clear only prefetch data (as I need to clear it after adding some other data)

clients.initialize(true); method worked for me

this always works for me to clear local/prefetched/remote data and reload it:

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

本文标签: javascriptCannot update twitter typeaheadjs prefetch thumbprint after initial loadStack Overflow