3

Let's suppose I want to use jQueryUi for implemeting autocomplete in a backboneView having a form.

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.:
the fetching should be performed just one time when the user types the first letter.

2 Answers 2

9

This should work and only call fetch once.

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)
    });
  }
});
Sign up to request clarification or add additional context in comments.

3 Comments

maybe did you miss to add this.collectionFetched = true somewhere?
Actually I did post another similar question because I would like to fetch the collection in autocomplete module. For more details Javascript module using jQueryUI.autocomplete and Backbone JS
This is going to hid getAutocomplete every time you focus on names. You might want to run that in initialize instead.
2

try like this :

var MyView = Backbone.View.extend({
    initialize: function () {
        this.myCollection = new MyCollection();

    },
    events: {
        'focus #names': 'getAutocomplete',
        'keydown #names':'invokefetch'
    },
    invokefetch : function(){
       this.myCollection.fetch(); 
       $("#names").unbind( "keydown", invokefetch);
    },    
    getAutocomplete: function () {
        $("#names").autocomplete({
            source: JSON.stringify(this.myCollection)
        });
    }
});

1 Comment

+1 thanks for your time. Actually I did post another similar question because I would like to fetch the collection in autocomplete module. For more details Javascript module using jQueryUI.autocomplete and Backbone JS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.