How to modify items returned from a Service and mapped using a mapper?

4 views
Skip to first unread message

Brian Hayward

unread,
Jan 20, 2013, 3:40:15 AM1/20/13
to mo...@googlegroups.com
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] {

        uri="http://sample.org/search?searchText=" + query
                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
        {
          alert ("Adding Item: " + it.name);
        }
}

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;
}

Zef Hemel

unread,
Jan 20, 2013, 8:35:51 AM1/20/13
to mobl group
It's hacky, but you can put a script { ... } block within a list, and put your if statement within that:

script {
 // if item is in my local list of items, set it.selected = true
        //     else set it.selected = false
        if(localItem(it))
        {
                it.selected = true;
        }
}

-- Zef

Brian Hayward

unread,
Jan 20, 2013, 2:06:38 PM1/20/13
to mo...@googlegroups.com
Thanks Zef, that took care of it!
--
Brian
Reply all
Reply to author
Forward
0 new messages