Sto usando Typeahead da Twitter. Mi sto imbattendo in questo avvertimento di Intellij. Ciò sta causando "window.location.href" per ciascun link come l'ultimo elemento nel mio elenco di elementi.La variabile mutabile è accessibile dalla chiusura. Come posso risolvere questo?
Come posso risolvere il mio codice?
Qui di seguito è il mio codice:
AutoSuggest.prototype.config = function() {
var me = this;
var comp, options;
var gotoUrl = "/{0}/{1}";
var imgurl = '<img src="/icon/{0}.gif"/>';
var target;
for (var i = 0; i < me.targets.length; i++) {
target = me.targets[i];
if ($("#" + target.inputId).length != 0) {
options = {
source: function (query, process) { // where to get the data
process(me.results);
},
// set max results to display
items: 10,
matcher: function (item) { // how to make sure the result select is correct/matching
// we check the query against the ticker then the company name
comp = me.map[item];
var symbol = comp.s.toLowerCase();
return (this.query.trim().toLowerCase() == symbol.substring(0, 1) ||
comp.c.toLowerCase().indexOf(this.query.trim().toLowerCase()) != -1);
},
highlighter: function (item) { // how to show the data
comp = me.map[item];
if (typeof comp === 'undefined') {
return "<span>No Match Found.</span>";
}
if (comp.t == 0) {
imgurl = comp.v;
} else if (comp.t == -1) {
imgurl = me.format(imgurl, "empty");
} else {
imgurl = me.format(imgurl, comp.t);
}
return "\n<span id='compVenue'>" + imgurl + "</span>" +
"\n<span id='compSymbol'><b>" + comp.s + "</b></span>" +
"\n<span id='compName'>" + comp.c + "</span>";
},
sorter: function (items) { // sort our results
if (items.length == 0) {
items.push(Object());
}
return items;
},
// the problem starts here when i start using target inside the functions
updater: function (item) { // what to do when item is selected
comp = me.map[item];
if (typeof comp === 'undefined') {
return this.query;
}
window.location.href = me.format(gotoUrl, comp.s, target.destination);
return item;
}
};
$("#" + target.inputId).typeahead(options);
// lastly, set up the functions for the buttons
$("#" + target.buttonId).click(function() {
window.location.href = me.format(gotoUrl, $("#" + target.inputId).val(), target.destination);
});
}
}
};
Con l'aiuto di @ cdhowie, ancora un po 'di codice: io aggiornare il programma di aggiornamento e anche il href per il click()
updater: (function (inner_target) { // what to do when item is selected
return function (item) {
comp = me.map[item];
if (typeof comp === 'undefined') {
return this.query;
}
window.location.href = me.format(gotoUrl, comp.s, inner_target.destination);
return item;
}}(target))};
Yao, (commento generale non legati alla la domanda): 'matcher' non include lo stesso' if (typeof comp === 'undefined') {...} 'test di sicurezza incluso in' evidenziatore' e 'updater'. In una rapida occhiata al codice, sembra che questa potrebbe essere una svista. –
@ Barbabietola-Barbabietola ooohhhhhh buona presa !!! grazie!!! sorprendentemente non ho ricevuto alcun errore quando non c'è corrispondenza =/ – iCodeLikeImDrunk
Yao, sembra che tu abbia una buona tolleranza agli errori in altre parti del tuo codice, ma "belt + braces" non fa mai male a nessuno. –