将 Typeahead 与 SignalR 结合使用
Using Typeahead with SignalR
我正在尝试将 Typeahead 与我的 SignalR 实现结合使用。
发生的事情是我的 Hub 被击中并返回值,但是 .done()
之后的 result
是 undifined
。我不知道为什么?
Javascript
$(function () {
var search = $.connection.searchHub;
$.connection.hub.start().done(function () {
$('#searchBar').typeahead(null, {
minLength: 2,
// begin source
source: function (query, process) {
var suggestions = [];// my callback value
search.server.search(query)
.done(function (result) {
console.log(result);
$.each(result, function () {
console.log(result);
suggestions.push(this);
process(suggestions);//process is a callback method
});
}).fail(function (error) {
console.log(error);
process([]);//process is a callback method, don't know if this is necessary here, but will produce no suggestions
});
}
});
});
中心:
[HubName("searchHub")]
public class SearchHub : Hub
{
public async Task Search(string query)
{
api = new MovieApi();
var result = await api.Search(query);
if (result.results != null)
{
Clients.Client(Context.ConnectionId).results(result.results[0].title);
}
else
{
Clients.Client(Context.ConnectionId).noResults("There are no search results!");
}
}
}
您的搜索方法没有 return 任何东西,所以它未定义并不奇怪。您需要更改为 Task<T>
和 return 某些内容
我也看不到你订阅的是results
或noResults
?喜欢
search.client.results = function(result) {
console.log(result);
};
编辑:为此使用 SIgnalR 也很奇怪,request/response 的标准 REST 在这里应该没问题
我正在尝试将 Typeahead 与我的 SignalR 实现结合使用。
发生的事情是我的 Hub 被击中并返回值,但是 .done()
之后的 result
是 undifined
。我不知道为什么?
Javascript
$(function () {
var search = $.connection.searchHub;
$.connection.hub.start().done(function () {
$('#searchBar').typeahead(null, {
minLength: 2,
// begin source
source: function (query, process) {
var suggestions = [];// my callback value
search.server.search(query)
.done(function (result) {
console.log(result);
$.each(result, function () {
console.log(result);
suggestions.push(this);
process(suggestions);//process is a callback method
});
}).fail(function (error) {
console.log(error);
process([]);//process is a callback method, don't know if this is necessary here, but will produce no suggestions
});
}
});
});
中心:
[HubName("searchHub")]
public class SearchHub : Hub
{
public async Task Search(string query)
{
api = new MovieApi();
var result = await api.Search(query);
if (result.results != null)
{
Clients.Client(Context.ConnectionId).results(result.results[0].title);
}
else
{
Clients.Client(Context.ConnectionId).noResults("There are no search results!");
}
}
}
您的搜索方法没有 return 任何东西,所以它未定义并不奇怪。您需要更改为 Task<T>
和 return 某些内容
我也看不到你订阅的是results
或noResults
?喜欢
search.client.results = function(result) {
console.log(result);
};
编辑:为此使用 SIgnalR 也很奇怪,request/response 的标准 REST 在这里应该没问题