I am writing an app where a user can search an online list of items and optionally select individual ones to add to the local database.
When searching, I want to offer a visual indicator that something in their search is already in their local DB.
Other ideas are welcome, but I'm trying to implement this with a checkbox on the search results that are returned.
I've highlighted the section I am trying to get to work in yellow. It doesn't seem like we can add logic like that within a list block, so are there any suggestions on another way I could accomplish this?
module searchitems
import mobl::ui::generic
import model
control searchitems()
{
header("Find Items")
var phrase = "ba"
var searchResult = async(Items.search(phrase))
searchBox(phrase, onsearch={searchResult = Items.search(phrase);})
whenLoaded(searchResult)
{
group {
list(it in searchResult) {
// if item is in my local list of items, set it.selected = true
// else set it.selected = false
if(localItem(it))
{
it.selected = true;
}
item {
checkBox(it.selected,
it.name + ":" +
it.id, {addLocalItem(it);})
}
}
}
}
}
service Items {
resource search(query: String) : [SearchItem] {
encoding="json"
mapper = searchItemMapper
}
}
function searchItemMapper (json : JSON) : [SearchItem] {
return json.results;
}
function addLocalItem(it : SearchItem)
{
// Will eventually add or remove from DB..
if (!it.selected)
{
alert ("Removing Item: " +
it.name);
}
else
{
}
}
function localItem(it : SearchItem) : Bool
{
// Stubbed out for now, will eventually check the local DB to see if the item already exists there.
return true;
}