Regenerate localsotrage data from a function
Hi everyone !!
I have a Typeahead created with the following configuration but I want to regerate the localstorage data from a javascript function, how I can I do it?
My typeahead:
$.typeahead({
input: '#creaProdSearch',
minLength: 1,
maxItem: 0,
accent: {
from: 'áéíóúÁÉÍÓÚ',
to: 'aeiouAEIOU'
},
order: "asc",
hint: true,
generateOnLoad: true,
cache: true,
compression: true,
template: "{{descripcion}} <br><small style='color:#999;'>Ref: {{refProducto}}</small>",
source: {
productos: {
display: ["descripcion","refProducto"],
ajax: function (query) {
var params = {
P_TEXTO: query,
P_EMPRESA: SavePedido.empresa,
}
return {
type: "POST",
url: "/crm/services/pedidos/productos/getProductos",
dataType: "json",
contentType: "application/json",
path: "rows",
data: JSON.stringify(params)
}
}
}
},
callback: {
onClickAfter: function (node, a, item, event) {
event.preventDefault();
}
}
});
My function to regenerate the typeahead:
function regenerate() {
localStorage.removeItem("TYPEAHEAD_#creaProdSearch:productos");
Typeahead["#creaProdSearch"].isGenerated = false;
Typeahead["#creaProdSearch"].source = {};
Typeahead["#creaProdSearch"].result = [];
Typeahead["#creaProdSearch"].resultCount = 0;
Typeahead["#creaProdSearch"].node.trigger('generateOnLoad.typeahead');
}
The above function delete the localstorage but not trigger the 'generateOnLoad.typeahead' method and, of course, not built again the localstorage and the Typeahead. What's wrong?
Thanks in advance.
Hi again !! I have solved so, but I don't know if this is a good solution.
var params = {
P_TEXTO: $("#creaProdSearch").val(),
P_EMPRESA: SavePedido.empresa,
}
function regenerate() {
localStorage.removeItem("TYPEAHEAD_#creaProdSearch:productos");
Typeahead["#creaProdSearch"].node.trigger('generate.creaprodsearch.typeahead');
}
The idea is to make a typeahead reset and reload data again (deleting the cache).
hi @anpegar yep this is a 2 step like your regenerate function does 1- clear storage 2- generate Typeahead source
ok, thanks.