Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API to refresh prefetched data on-demand? #223

Closed
billdami opened this issue May 2, 2013 · 4 comments
Closed

API to refresh prefetched data on-demand? #223

billdami opened this issue May 2, 2013 · 4 comments
Milestone

Comments

@billdami
Copy link

billdami commented May 2, 2013

I read through the docs and did some searching but wasn't really able to find a definitive answer on this; is their a way to programmatically clear prefetched data stored in localStorage from some arbitrary point in my application's logic?

For instance, I have a wiki-type application that allows the users to create/modify articles, and the data source of the typeahead is that same collection of articles. When the user creates an article and it gets saved to the server, it would be expected that the article would instantly show up in the typeahead results, so I would want a way to essentially refresh the cached dataset at that specific point. It would also be nice to be able to do this periodically while the user has the app open, by checking the server to see if its locally cached data is outdated and refresh it before the default expire time (which is a few days or so) is reached.

If this is not do-able via the typeahead's API, is there another workaround such as removing the specific dataset from localStorage manually, so that when the typeahead instance looks for it and it is not there, it at that point re-fetches it?

@elfasd
Copy link

elfasd commented May 2, 2013

Ahm... I am expeting for the same thing.....

@jharding
Copy link
Contributor

jharding commented May 2, 2013

Your best bet may be to define datasets without a name property. This will prevent typeahead.js from storing prefetched data in localStorage. Then whenever the user saves an article, you could destroy/reinitialize the typeahead with the new data. Something like:

function onSave() {
  // ...
  $('.typeahead')
  .typeahead('destroy')
  .typeahead({ prefetch: '/data.json' });
}

@billdami
Copy link
Author

billdami commented May 2, 2013

@jharding Yeah, I did see that mentioned in other issues regarding this topic, however I was hoping to retain the localStorage feature of the typeahead, because this is a round-trip application, so disabling the localStorage would mean that on every page load the data would have to be re-prefetched from the server. And since there will definitely be considerable lengths of time when the user is just navigating between pages whilst making no changes to the dataset, making a request for the data on every page load is just an unnecessary burden on the server.

I noticed in the PersistentStorage object there is a clear method, if I implemented something similar to remove the typeahead's data in window.localStorage myself would this cause typeahead to fetch the data from the server on the next search action, or would that cause some unwanted side effects?

@jharding
Copy link
Contributor

jharding commented May 2, 2013

The only time data is fetched from the remote endpoint is on initialization so clearing localStorage wouldn't help.

Honestly, right now there isn't a really great way of implementing what your asking about. The best way to go about this is to probably build something into typeahead.js for refreshing datasets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants