通过引用传递/存储数组?
我对 javascript 还很陌生。我下载了一些代码来为搜索框制作自动完成器,但它没有按我的预期工作。
这是我下载的代码:
Autocompleter.Local = new Class({
Extends: Autocompleter,
initialize: function (element, tokens, options) {
this.parent(element, options);
console.log(tokens); // I added this
this.tokens = tokens;
},
query: function () {
console.log(this.tokens); // and this
this.update(this.filter());
}
});
为了使用此代码,我这样做:
var Activities = ['aa', 'bb', 'cc', 'abcd'];
function InitializePage() {
new Autocompleter.Local('txtSearch', Activities, {
'minLength': 1, // We wait for at least one character
'overflow': false, // Overflow for more entries
'selectMode': 'type-ahead'
});
}
window.addEvent('domready', function () {
InitializePage();
});
初始化函数的 tokens
参数是自动完成程序可能建议的所有可能内容的数组。 filter()
函数会过滤这些内容,以便仅向用户显示相关的内容。我想要做的是在不同时间更改 tokens
数组,以便向用户建议不同的内容。
我尝试这样做:
new Request.JSON({ method: 'get', url: 'handler/GetActivities.ashx', autoCancel: true, urlEncoded: false, secure: false,
headers: { "Content-type": "application/json" },
onSuccess: function (_json, _jsonText) {
Activities = _json;
}
}).send();
我已确认此请求正在更新 Activities
变量。但是,自动完成器类中的 tokens
变量仍保留为其初始化时的数组。
如何获取它以使 tokens
变量指向与 activities
变量相同的数据?
I'm fairly new to javascript. I downloaded some code to make an autocompleter for search boxes, but it's not working as I expected it.
This is the code that I downloaded:
Autocompleter.Local = new Class({
Extends: Autocompleter,
initialize: function (element, tokens, options) {
this.parent(element, options);
console.log(tokens); // I added this
this.tokens = tokens;
},
query: function () {
console.log(this.tokens); // and this
this.update(this.filter());
}
});
To make use of this code, I do this:
var Activities = ['aa', 'bb', 'cc', 'abcd'];
function InitializePage() {
new Autocompleter.Local('txtSearch', Activities, {
'minLength': 1, // We wait for at least one character
'overflow': false, // Overflow for more entries
'selectMode': 'type-ahead'
});
}
window.addEvent('domready', function () {
InitializePage();
});
The tokens
parameter for the initialize function is an array of all the possible things that the autocompleter might suggest. the filter()
function filters these so only relevant ones are displayed to the user. What I want to do is change the tokens
array at various times so different things are suggested to the user.
I tried doing this:
new Request.JSON({ method: 'get', url: 'handler/GetActivities.ashx', autoCancel: true, urlEncoded: false, secure: false,
headers: { "Content-type": "application/json" },
onSuccess: function (_json, _jsonText) {
Activities = _json;
}
}).send();
I have confirmed that the Activities
variable is being updated by this request. However, the tokens
variable inside the autocompleter class remains as the array that it was initialized as.
How do I get it so that the tokens
variable points to the same data as the activities
variable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您正在覆盖数组。您应该替换这些元素。您可以使用
splice
[MDN]:这将删除所有元素(从
0
到Activities.length
)并添加每个元素_json
数组。但是
_json
可能包含未知数量的元素,像我们上面那样对其进行硬编码是非常糟糕的。幸运的是,我们可以使用apply
[MDN] 方法调用具有可变数量参数的方法,以数组形式提供:这与上面的调用等效,但添加了
_json
。参考:
concat
,拼接
、申请
The problem is that you are overriding the array. You should replace the elements instead. You can add and remove elements in-place with
splice
[MDN]:This would remove all elements (from
0
toActivities.length
) and add each element from the_json
array.But
_json
might contain an unknown number of elements, and hardcoding it like we did above is very bad. Fortunately, we can use theapply
[MDN] method to call a method with a variable number of arguments, provided as array:This is the equivalent to the call above, but adding all elements from
_json
.Reference:
concat
,splice
,apply