Twitter的输入型猎犬:使用ajax.data和POST时,“%QUERY”相当于什么?

时间:2014-06-08 16:57:58

标签: jquery post twitter-typeahead bloodhound

如果使用Bloodhound和GET:

// Typeahead
personsBloodhound = new Bloodhound({
    datumTokenizer: function (person) { return person.name; },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    remote: {
        url: '/ajax/Persons/List?nameContains=%QUERY',
        ajax: {
            beforeSend: function(xhr) {
                $(".searching-person").show();
            },
            data: {
                "pageSize": 4,
                "otherParam1": "blah",
                "otherParam2": "bleh",
            }
        },
        filter: function (response) {
            $(".searching-person").hide();
            return response.persons;
        }
    }
});

只需在网址中使用%QUERY即可。

现在....
如果使用Bloodhound和POST,我应该使用什么而不是%QUERY?

// Typeahead
personsBloodhound = new Bloodhound({
    datumTokenizer: function (person) { return person.name; },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    remote: {
        url: '/ajax/Persons/List',
        ajax: {
            type: "POST",
            beforeSend: function(xhr) {
                $(".searching-person").show();
            },
            data: {
                "nameContains": ....WHAT GOES HERE?????......
                "pageSize": 4,
                "otherParam1": "blah",
                "otherParam2": "bleh",
            }
        },
        filter: function (response) {
            $(".searching-person").hide();
            return response.persons;
        }
    }
});

如果不清楚,问题是:
在Bloodhound的遥控器中使用POST时,%QUERY相当于什么?

文档对此不清楚,(证据): https://github.com/twitter/typeahead.js/blob/master/doc/bloodhound.md#remote

还尝试使用:

 "nameContains": $("#my-input-that-uses-typeahead").val(),

但没有工作。

3 个答案:

答案 0 :(得分:0)

是的,在进一步观察猎犬之后,通配符就是取代而不是价值的东西。

它不会将查询字符串存储在任何位置。在queryChanged点击并过滤远程响应。看起来你自己必须得到这个问题。

"nameContains": $('input#search').val()

答案 1 :(得分:0)

你必须以某种方式改变URL,否则血腥的人不会发送其他请求(参见https://stackoverflow.com/a/24025789/2175370)而livesearch / typeahead将无法工作。

因此,("#my-input-that-uses-typeahead").val()可以与ajax设置中的动态网址(例如http://127.0.0.1:1234/REST_API/_search?useless=%QUERY)和beforeSend - 功能结合使用。

我要提出有关此行为的问题。使用Bloodhound进行POST请求非常尴尬,并且消除了typeahead.js的简单性。

编辑:

另外,请务必在beforeSend中设置数据的新值并设置settings.hasContent = true否则将使用初始数据。

如何完成的示例:https://github.com/twitter/typeahead.js/issues/542#issuecomment-29995960

答案 2 :(得分:0)

在Bloodhound的遥控器中使用POST时,%QUERY的等效值为query

以下是一个简单示例(详细说明),您可以将其用于GETPOST。如您所见,我已声明变量isExtendUrl。如果将其设置为true,则查询(您键入的内容)将添加到网址的末尾(您必须以某种方式提供myurl)。

下一个变量是isRequestMethod。如果此设置为POST,您可以使用猎犬进行POST来电,否则您可以将其用于GET来电。如您所见,prepare函数有两个参数querysettingquery是您键入的内容。如果您只想在POST对象内GET调用prepare移动remote键值对。

因此,如果您必须使用JSON正文{gender: 'MALE', name: 'what is typed'}作为POST来电。您可以拥有一个包含所有键值对的初始查询对象,例如:initialQuery = {gender: 'MALE'},以及在搜索时应添加到searchKey的键initialQuery,可以添加到{{1} }像prepare

最后,如果initialQuery[searchKey] = query调用的响应对象是对象,并且您必须使用POST提取特定键值。例如:说你的回应对象是

filter

,您必须获得{ status: 'some status', content: [{array}, {of}, {objects}, ...], someKey: someValue } ,然后返回content。这是一个完整的例子

data.content