2

Let's suppose I want to use jQueryUi.autocomplete for making a module which take the source from a backboneCollection.

I implement the following code (1) for the autocomplete module and
the following for the Backbone.view (2).

Actually, 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 or the source function only when the user starts to type something in the input box?


P.S.:
I have already posted a similar question jQuery Autocomplete Plugin using Backbone JS , but since the needs of aoutocomplete module could be shared between different view I decided to move the fetch of the collection in autocomplete module.


(1)

/*global define */
define([
    'users',
    'jquery',
    'jqueryUi'
], function (UserCollection, $) {
    "use strict";
    var autoComplete = function(element) {

        var userCollection,
            data;

        userCollection = new UserCollection(); 
        userCollection.fetch();

        data =  userCollection.toJSON();

        element.autocomplete({
            minLength: 3,
            source: function(request, response) {
                var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
                response($.grep(data, function(value) {
                    return matcher.test(value.name);
                }));
            },
            create: function() {
                element.val(data.name);
            },
            focus: function(event, ui) {
                element.val(ui.item.name);
                return false;
            },
            select: function(event, ui) {
                element.val(ui.item.name);
                return false;
            }
        }).data('autocomplete')._renderItem = function(ul, item) {
            return $('<li></li>')
                .data('item.autocomplete', item)
                .append('<a><img src="' + item.avatar + '" />' + item.name + '<br></a>')
                .appendTo(ul);
        };
    };

    return autoComplete;
});

(2)

// View1 (view using the module autocomplete)
define([
    'autoCompleteModule'
], function (autoCompleteModule) {
    var MyView = Backbone.View.extend({

        events: {
            'focus #names': 'getAutocomplete'
        },
        getAutocomplete: function (e) {
            autoCompleteModule($('#names'));
        }
    });
});

1 Answer 1

2

//Pseudo code from jQuery Autocomplete Plugin using Backbone JS

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)
    });
}

});

EDIT-20120711--------- how about this:

//Pseudo code ...

// View1 (view using the module autocomplete)
define([
    'autoCompleteModule'
], function (autoCompleteModule) {
    var MyView = Backbone.View.extend({

        events: {
            'keydown #names': 'getAutocomplete'
        },
        getAutocomplete: function (e) {
            var el = $('#names');
            if(!el.data){
                autoCompleteModule(el);
            }
        }
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, +1 for your idea...Actually what I am asking is a little different. Your pseudo code works great if you have just one view using autocomplete and myCollection. Let's suppose you have two views using the same myCollection you will have two fetch for the same collection. For this reason I created a module for autocomplete.
@LorraineBernard key,I updated the answer, see if it is not what you want.

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.