本文介绍了使用JS骨干jQuery的自动完成插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让我们假设我想使用jQueryUI的具有一种形式,一个backboneView implemeting自动完成。
Let's suppose I want to use jQueryUi for implemeting autocomplete in a backboneView having a form.
我实现以下code(*),
但由于还进行集合的抓取,当用户不输入任何字母,我不喜欢它。
I implement the following code (*),but I don't like it because the fetching of the collection is performed also when the user does not type any letter.
我应该如何只有当用户开始输入,在输入框中的东西进行抓取集合?
How should I perform the fetching collection only when the user starts to type something in the input box?
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)
});
}
});
P.S:结果
在取应执行只是一个时间当用户键入的第一个字母。
P.S.:
the fetching should be performed just one time when the user types the first letter.
推荐答案
这应该和来电取一次。
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)
});
}
});
这篇关于使用JS骨干jQuery的自动完成插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!