Select2查询功能:回调为空

时间:2014-08-04 20:03:57

标签: javascript jquery jquery-select2

我使用Select2组件查询功能进行Ajax调用。

我使用jQuery .when()通过query.callback(myMasterCollection)发回数据。

基本上,我想用每个远程调用填充这个Master对象(因为我将添加更多)并在回调中将其传回。

我试图通过关闭来访问Master对象。也许这就是我出错的地方。

问题是我可以清楚地看到,在Ajax调用本身内部,数据和Master集合一起被填充....

..但是在我执行query.callback(myMasterCollection)的.when()中,它是空的。为什么?

以下是完整的查询功能:

query: function(query){
this.searchResults = {results : []};

this.rosterResults = {displayName:'CONTACTS',children:[]};
this.dirResults    = {displayName:'DIRECTORY',children:[]};
this.groupResults  = {displayName:'GROUPS',children:[]};

var $this = this;

// Search the roster first
var filteredRoster = new Array();

filteredRoster = $.grep(remainingPeople, function(value, i){
    return (value.displayName.toString().toLowerCase().indexOf(query.term.toLowerCase()) > -1 || value.contactName.toString().toLowerCase().indexOf(query.term.toLowerCase()) > -1);
});

if (filteredRoster.length > 0){

    for ( var i = 0; i < filteredRoster.length; i++){

        var isAlreadyAdded = false;
        for (var j = 0; j < participants.length; j++){
            if (filteredRoster[i].contactName == participants[j].id){
                isAlreadyAdded = true;
                break;
            }
        }

        if (!isAlreadyAdded && filteredRoster[i].contactName != getOnBehalfOf()){
            this.rosterResults.children.push({
                id : filteredRoster[i].contactName,
                displayName : filteredRoster[i].displayName,
                avatar: filteredRoster[i].avatar,
                title: filteredRoster[i].title,
                companyName: filteredRoster[i].companyName,
                type : 0
            });
        }
    }
    this.searchResults.results.push( this.rosterResults );
} else{ 
    // Search the directory
    var dirSearch = myWrapper.ajax("/app/search", {query: query.term}, function(data, textStatus, jqXHR){

        if (data.response.userList.user){

            var peeps = data.response.userList.user;

            for (var i = 0; i < peeps.length; i++){

                var isAlreadyAdded = false;
                for (var j = 0; j < participants.length; j++){
                    if (peeps[i].loginName == participants[j].id){
                        isAlreadyAdded = true;
                        break;
                    }
                }

                if (!isAlreadyAdded && peeps[i].loginName != getOnBehalfOf()){
                    $this.dirResults.children.push({
                        id : peeps[i].loginName,
                        displayName : peeps[i].displayName,
                        avatar: peeps[i].avatar,
                        title: peeps[i].title,
                        companyName: peeps[i].companyName,
                        type : 1
                    });
                }
            }
            $this.searchResults.results.push( $this.dirResults );   
        }
    });

    // Search for Contact Groups
    var groupSearch = myWrapper.ajax("/api/searchGroups", {q: query.term}, function(data, textStatus, jqXHR){

        if (data.response.userList.user){

            var peeps = data.response.userList.user;

            for (var i = 0; i < peeps.length; i++){

                var isAlreadyAdded = false;
                for (var j = 0; j < participants.length; j++){
                    if (peeps[i].loginName == participants[j].id){
                        isAlreadyAdded = true;
                        break;
                    }
                }

                if (!isAlreadyAdded && peeps[i].loginName != getOnBehalfOf()){
                    $this.groupResults.children.push({
                        id : peeps[i].loginName,
                        displayName : peeps[i].displayName,
                        avatar: peeps[i].avatar,
                        title: peeps[i].title,
                        companyName: peeps[i].companyName,
                        type : 1
                    });

                }
            }
            $this.searchResults.results.push( $this.groupResults );
        } 
    });
}
$.when(dirSearch).done(function(result1) {
    console.log("CALLBACK");
    query.callback( $this.searchResults );
});

0 个答案:

没有答案