Probably a scope problem that I don't understand

Hi.

Actually, I write a code that works but I think is not correct.

I make a network.request and in the event handler I put in options params the response data that I pass on another Scene.

Since from that Scene I have to made two network.request I want to call them on the proper button touch handler, but if I do that no data is passed.

local function handleGolfClubResponse( event ) if not event.isError then local response = json.decode( event.response ) if response.status == “OK” then h = #response.data for i=1,h do table.insert(options.params, {nome = response.data[i].nome, descrizione = response.data[i].descrizione, immagine = response.data[i].immagine, data_fine = response.data[i].data_fine, data_validita = response.data[i].validita, prezzo= response.data[i].prezzo, listino = response.data[i].listino }) end end else print( “Error!” ) end return end

network.request( “http://codelessfuture9892.cloudapp.net/api/itinerari”, “GET”, handleItinerariResponse, {} )

local function onObjectTouch( event )
 if ( event.phase == “ended”) then 
  if ( event.target.id == “Bundle”) then
   changeTabBar()

– if I put network.request here, in bundle.lua I got error
    composer.gotoScene( “bundle”, options ) – NOW IS OK
  elseif ( event.target.id == “Club”) then
   changeTabBar()
    composer.gotoScene( “club”, options )
  elseif ( event.target.id == “Search”) then
   print( “Search” )      
  end
  return true
 end
end

I post a link https://gist.github.com/CodelessFuture/39bb6d0e30c82188a4245b327f807aec

Formatting problems sorry

First I would make options “local” at the top.  You don’t need that to be global and it could cause issues with your other scenes.

Then I think you need to be adding a table to params.  Maybe something like:

h = #response.data options.params.data = {} for i=1,h do options.params.data[i] = {nome = response.data[i].nome, descrizione = response.data[i].descrizione, immagine = response.data[i].immagine, data\_fine = response.data[i].data\_fine, data\_validita = response.data[i].validita, prezzo= response.data[i].prezzo, listino = response.data[i].listino } end

Or something like that.  That way you’re passing a single table named “data” to params instead of many “params” records. The code is untested, but something like that should work for you.

Rob

I have to better 

This  doesn’t  work

local function handleItinerariResponse( event )
    if not event.isError then
        local response = json.decode( event.response )
        if response.status == “OK” then
            h = #response.data
            for i=1,h do
                table.insert(options.params, {nome = response.data[i].nome,
                    descrizione = response.data[i].descrizione,
                    immagine = response.data[i].immagine,
                    data_fine = response.data[i].data_fine,
                    data_validita = response.data[i].validita,
                    prezzo= response.data[i].prezzo,
                    listino = response.data[i].listino
               })
         end
        end
    else
        print( “Error!” )
    end
    return
end

local function handleGolfClubResponse( event )
    if not event.isError then
        local response = json.decode( event.response )
        if response.status == “OK” then
         h = #response.data
         for i=1,h do
       table.insert(options.params, {nome = response.data[i].nome,
                descrizione = response.data[i].descrizione,
                immagine = response.data[i].immagine,
                data_fine = response.data[i].data_fine,
                data_validita = response.data[i].validita,
                prezzo= response.data[i].prezzo,
                listino = response.data[i].listino
                })
         end
        end
    else
        print( “Error!” )
    end
    return
end

local function onObjectTouch( event )
 if ( event.phase == “ended”) then 
  if ( event.target.id == “Bundle”) then

  – I call API here
   network.request( “http://codelessfuture9892.cloudapp.net/api/itinerari”, “GET”, handleItinerariResponse, {} ) 

   changeTabBar()
    composer.gotoScene( “bundle”, options )
  elseif ( event.target.id == “Club”) then

  – I call API here
   network.request( “http://codelessfuture9892.cloudapp.net/api/golfclubs”, “GET”, handleGolfClubResponse )
   changeTabBar()
    composer.gotoScene( “club”, options )
  elseif ( event.target.id == “Search”) then
   print( “Search” )      
  end
  return true
  end
end

This instead work but give me problems…

– I made the calls outside

network.request( “http://codelessfuture9892.cloudapp.net/api/itinerari”, “GET”, handleItinerariResponse )
network.request( “http://codelessfuture9892.cloudapp.net/api/golfclubs”, “GET”, handleGolfClubResponse )

local function onObjectTouch( event )
 if ( event.phase == “ended”) then 
  if ( event.target.id == “Bundle”) then
   changeTabBar()
    composer.gotoScene( “bundle”, options )
  elseif ( event.target.id == “Club”) then
   changeTabBar()
    composer.gotoScene( “club”, options )
  elseif ( event.target.id == “Search”) then
   print( “Search” )      
  end
  return true
 end
end

Yes actually i did something like this for let this working, bun it the next scene I have to call network.request from the handler because I have to made an api call from the id of the selected item…

I post a link https://gist.github.com/CodelessFuture/39bb6d0e30c82188a4245b327f807aec

Formatting problems sorry

First I would make options “local” at the top.  You don’t need that to be global and it could cause issues with your other scenes.

Then I think you need to be adding a table to params.  Maybe something like:

h = #response.data options.params.data = {} for i=1,h do options.params.data[i] = {nome = response.data[i].nome, descrizione = response.data[i].descrizione, immagine = response.data[i].immagine, data\_fine = response.data[i].data\_fine, data\_validita = response.data[i].validita, prezzo= response.data[i].prezzo, listino = response.data[i].listino } end

Or something like that.  That way you’re passing a single table named “data” to params instead of many “params” records. The code is untested, but something like that should work for you.

Rob

I have to better 

This  doesn’t  work

local function handleItinerariResponse( event )
    if not event.isError then
        local response = json.decode( event.response )
        if response.status == “OK” then
            h = #response.data
            for i=1,h do
                table.insert(options.params, {nome = response.data[i].nome,
                    descrizione = response.data[i].descrizione,
                    immagine = response.data[i].immagine,
                    data_fine = response.data[i].data_fine,
                    data_validita = response.data[i].validita,
                    prezzo= response.data[i].prezzo,
                    listino = response.data[i].listino
               })
         end
        end
    else
        print( “Error!” )
    end
    return
end

local function handleGolfClubResponse( event )
    if not event.isError then
        local response = json.decode( event.response )
        if response.status == “OK” then
         h = #response.data
         for i=1,h do
       table.insert(options.params, {nome = response.data[i].nome,
                descrizione = response.data[i].descrizione,
                immagine = response.data[i].immagine,
                data_fine = response.data[i].data_fine,
                data_validita = response.data[i].validita,
                prezzo= response.data[i].prezzo,
                listino = response.data[i].listino
                })
         end
        end
    else
        print( “Error!” )
    end
    return
end

local function onObjectTouch( event )
 if ( event.phase == “ended”) then 
  if ( event.target.id == “Bundle”) then

  – I call API here
   network.request( “http://codelessfuture9892.cloudapp.net/api/itinerari”, “GET”, handleItinerariResponse, {} ) 

   changeTabBar()
    composer.gotoScene( “bundle”, options )
  elseif ( event.target.id == “Club”) then

  – I call API here
   network.request( “http://codelessfuture9892.cloudapp.net/api/golfclubs”, “GET”, handleGolfClubResponse )
   changeTabBar()
    composer.gotoScene( “club”, options )
  elseif ( event.target.id == “Search”) then
   print( “Search” )      
  end
  return true
  end
end

This instead work but give me problems…

– I made the calls outside

network.request( “http://codelessfuture9892.cloudapp.net/api/itinerari”, “GET”, handleItinerariResponse )
network.request( “http://codelessfuture9892.cloudapp.net/api/golfclubs”, “GET”, handleGolfClubResponse )

local function onObjectTouch( event )
 if ( event.phase == “ended”) then 
  if ( event.target.id == “Bundle”) then
   changeTabBar()
    composer.gotoScene( “bundle”, options )
  elseif ( event.target.id == “Club”) then
   changeTabBar()
    composer.gotoScene( “club”, options )
  elseif ( event.target.id == “Search”) then
   print( “Search” )      
  end
  return true
 end
end

Yes actually i did something like this for let this working, bun it the next scene I have to call network.request from the handler because I have to made an api call from the id of the selected item…