Very basic error handling

develop
Sven Slootweg 12 years ago
parent 6ec1a2d90b
commit dd4c62bc4e

@ -36,14 +36,21 @@ else
array_unshift($db_query_arguments, ''); array_unshift($db_query_arguments, '');
unset($db_query_arguments[0]); unset($db_query_arguments[0]);
$results_topics = Topic::CreateFromQuery("SELECT * FROM topics WHERE {$db_query}", $db_query_arguments); try
{
$return_objects = array(); $results_topics = Topic::CreateFromQuery("SELECT * FROM topics WHERE {$db_query}", $db_query_arguments);
$return_objects = array();
foreach($results_topics as $topic) foreach($results_topics as $topic)
{
$return_objects[] = $topic->AsDataset();
}
$sPageContents = json_encode($return_objects);
}
catch (NotFoundException $e)
{ {
$return_objects[] = $topic->AsDataset(); $sPageContents = json_encode(array("error" => "No results found for the specified query.", "query" => $query));
} }
$sPageContents = json_encode($return_objects);
} }

@ -136,3 +136,9 @@ a.title
{ {
content: ")"; content: ")";
} }
.error
{
margin: 8px 16px;
font-size: 19px;
}

@ -11,7 +11,7 @@
/*$("input").val("data"); /*$("input").val("data");
runSearch();*/ runSearch();*/
$("input").keypress(function(){ $("input").keyup(function(){
if(typeof search_timeout !== "null") if(typeof search_timeout !== "null")
{ {
clearTimeout(search_timeout); clearTimeout(search_timeout);
@ -27,38 +27,53 @@
$(".spinner").show(); $(".spinner").show();
var query = $("input#query").val(); var query = $("input#query").val();
$.post("/api/search", {q: query}, function(response){ if(query.length >= 3)
$(".spinner").hide(); {
$(".results").html(""); $.post("/api/search", {q: query}, function(response){
$(".spinner").hide();
for(i in response) $(".results").html("");
{
if(response[i].items.length > 0) if(typeof response.error == "undefined")
{ {
var result_wrapper = instantiateTemplate("result_wrapper"); for(i in response)
var result_block = instantiateTemplate("result_topic");
result_block.children(".title").html(response[i].title);
result_block.children(".providername").html(response[i].provider);
result_block.appendTo(result_wrapper);
for(x in response[i].items)
{ {
item = response[i].items[x]; if(response[i].items.length > 0)
{
var item_block = instantiateTemplate("result_item"); var result_wrapper = instantiateTemplate("result_wrapper");
item_block.children(".title").html(item.title);
item_block.children(".title").attr("href", item.url); var result_block = instantiateTemplate("result_topic");
item_block.children(".type").html(item.type); result_block.children(".title").html(response[i].title);
item_block.insertAfter(result_block); result_block.children(".providername").html(response[i].provider);
result_block.appendTo(result_wrapper);
for(x in response[i].items)
{
item = response[i].items[x];
var item_block = instantiateTemplate("result_item");
item_block.children(".title").html(item.title);
item_block.children(".title").attr("href", item.url);
item_block.children(".type").html(item.type);
item_block.insertAfter(result_block);
}
result_wrapper.appendTo(".results");
}
} }
result_wrapper.appendTo(".results");
} }
} else
{
setHandlers(); $(".results").html("<div class='error'>No results.</div>");
}, "json"); }
setHandlers();
}, "json");
}
else
{
$(".spinner").hide();
$(".results").html("<div class='error'>Enter at least 3 characters.</div>");
}
} }
function setHandlers() function setHandlers()

Loading…
Cancel
Save