From dd4c62bc4ed8a038211dec16ece214d037c8ad25 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Mon, 28 Jan 2013 15:43:39 +0100 Subject: [PATCH] Very basic error handling --- frontend/modules/api/search.php | 21 ++++++---- frontend/style.css | 6 +++ frontend/templates/ui/index.tpl | 73 ++++++++++++++++++++------------- 3 files changed, 64 insertions(+), 36 deletions(-) diff --git a/frontend/modules/api/search.php b/frontend/modules/api/search.php index 2dc1349..6a593e1 100644 --- a/frontend/modules/api/search.php +++ b/frontend/modules/api/search.php @@ -36,14 +36,21 @@ else array_unshift($db_query_arguments, ''); unset($db_query_arguments[0]); - $results_topics = Topic::CreateFromQuery("SELECT * FROM topics WHERE {$db_query}", $db_query_arguments); - - $return_objects = array(); + try + { + $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); } diff --git a/frontend/style.css b/frontend/style.css index 0a8d88e..8e087ed 100644 --- a/frontend/style.css +++ b/frontend/style.css @@ -136,3 +136,9 @@ a.title { content: ")"; } + +.error +{ + margin: 8px 16px; + font-size: 19px; +} diff --git a/frontend/templates/ui/index.tpl b/frontend/templates/ui/index.tpl index 6c77c1d..b2a5c35 100644 --- a/frontend/templates/ui/index.tpl +++ b/frontend/templates/ui/index.tpl @@ -11,7 +11,7 @@ /*$("input").val("data"); runSearch();*/ - $("input").keypress(function(){ + $("input").keyup(function(){ if(typeof search_timeout !== "null") { clearTimeout(search_timeout); @@ -27,38 +27,53 @@ $(".spinner").show(); var query = $("input#query").val(); - $.post("/api/search", {q: query}, function(response){ - $(".spinner").hide(); - $(".results").html(""); - - for(i in response) - { - if(response[i].items.length > 0) + if(query.length >= 3) + { + $.post("/api/search", {q: query}, function(response){ + $(".spinner").hide(); + $(".results").html(""); + + if(typeof response.error == "undefined") { - var result_wrapper = instantiateTemplate("result_wrapper"); - - 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) + for(i in response) { - 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); + if(response[i].items.length > 0) + { + var result_wrapper = instantiateTemplate("result_wrapper"); + + 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]; + + 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"); } - } - - setHandlers(); - }, "json"); + else + { + $(".results").html("
No results.
"); + } + + setHandlers(); + }, "json"); + } + else + { + $(".spinner").hide(); + $(".results").html("
Enter at least 3 characters.
"); + } } function setHandlers()