gpt4 book ai didi

javascript - 使用 Backbone JS 的 jQuery 自动完成插件

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:05:06 34 4
gpt4 key购买 nike

假设我想使用 jQueryUi 在具有表单的 backboneView 中实现自动完成。

我实现了下面的代码(*),但我不喜欢它,因为当用户不输入任何字母时也会执行集合的获取。

只有当用户开始在输入框中输入内容时,我应该如何进行抓取收集?

var MyView = Backbone.View.extend({
initialize: function () {
this.myCollection = new MyCollection();
this.myCollection.fetch(); // I would like to fetch the collection
// only when the user start to type the first letter
},
events: {
'focus #names': 'getAutocomplete'
},

getAutocomplete: function () {
$("#names").autocomplete({
source: JSON.stringify(this.myCollection)
});
}
});

附言:
当用户键入第一个字母时,应该只执行一次提取。

最佳答案

这应该可以工作并且只调用 fetch 一次。

var MyView = Backbone.View.extend({
initialize: function () {
this.myCollection = new MyCollection();
this.collectionFetched = false;
},
events: {
'focus #names': 'getAutocomplete'
'keydown #names': 'fetchCollection'
},
fetchCollection: function() {
if (this.collectionFetched) return;
this.myCollection.fetch();
this.collectionFetched = true;
},
getAutocomplete: function () {
$("#names").autocomplete({
source: JSON.stringify(this.myCollection)
});
}
});

关于javascript - 使用 Backbone JS 的 jQuery 自动完成插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11413430/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com