Understanding accessing parent object using overlays

Hi all,

I am bulding a biz app and I am stuck towards later stage of my development. Let me briefly explain the scenario - the app is try to set modifiers in an overlay screen (using showOverlay) for record in a tableView in the parent screen.

An analogy to showcase my example - my parent screen has a table view, each record having a Pizza. On click of one of the record (Pizza) an overlay scene is called which gives the user some options to add/modify pizza attributes (for eg: add Jalapenos, add Cheese, sauce, etc). Once the attributes are selected, the user will save/cancel and go back to the parent screen and the attributes are associated to the parent record (pizza).

Again using the same example, the issue I am facing is not being able to access my table (array) variable which consists of record of pizza along with it’s attributes in overlay screen. I would also like to modify the values of attributes in parent scene from the overlay screen based on what is selected in the overlay screen.

As far as I have read and understood, event.parent is passed on to the overlay scene so that variables of the parent screen are accessible in overlay screen.

Am I correct in my understanding.

Following sample pseudo code will perhaps explain the issue:

– parent scene

basketItemAttributes = {}

– somewhere in the code a loop to populate records and attributes in the above table
basketItemAttributes[Seq] = {}
basketItemAttributes[Seq].prodName = dbRow.prodName
basketItemAttributes[Seq].qty = dbRow.qty
basketItemAttributes[Seq].rate = dbRow.rate

basketItemAttributes[Seq].modifierName1 =""
basketItemAttributes[Seq].selectedModifier1 =""
basketItemAttributes[Seq].modifierName2 =""
basketItemAttributes[Seq].selectedModifier2 =""
basketItemAttributes[Seq].modifierName3 =""
basketItemAttributes[Seq].selectedModifier3 =""

– note the modifier/attributes are blank at this state as i would like to populate them from overlay screen based on what is selected

– somewhere below
composer.showOverlay(“modifiers”,options)

–modifiers.lua

– a screen with table of modifier options and on selection of a modifier be able to populate the same in variable of parent screen

– for eg:

event.parent.basketItemAttributes[Seq].modifierName1 =“Sauce”
basketItemAttributes[displaySeq].selectedModifier1 =“Tomato”

event.parent.basketItemAttributes[Seq].modifierName1 =“Cheese”
basketItemAttributes[displaySeq].selectedModifier1 =“Cheddar”

The issue is I am not even be able to access the parent variable in overlay screen. For eg: print(basketItemAttributes[Seq].prodName) gives me an error "attempt to index local parent (a nil value).

Can someone help me please. Ask any questions if somethings not clear.

Thanks,

Where are you accessing the event.parent which function/event listener? Are you using Composer or Storyboard?

Ideally I would like to use event.parent somewhere later but just for test sake, I have it in function scene:create(event) of my overlay scene. I am using composer.

Thanks

In your parent scene, if you do:  scene.basketItemAttributes = {}

instead of:  basketItemAttributes = {}

Then in your overlay you can do event.parent.basketItemAttributes[whatever] to access the table entries.

Rob
 

You can also pass reference to your array to overlay using params option of showOverlay function.

composer.showOverlay("modifiers", {    params = {       basketItemAttributes = basketItemAttributes    } });

which you can access form overlay create function using event.params.basketItemAttributes

or even better you can pass function to overlay:

local function OnOverlaySave(data)    -- modify pizza and save end composer.showOverlay("modifiers", {    params = {       onSaveFn = OnOverlaySave    } });

in overlay you will just need to collect all data and call the function event.params.onSaveFn(data);

Thanks Rob and Hayk. Your solution worked like a charm.

I have one more small issue.

In the same example the parent scene has a tableview with a list of pizza’s and on a click of one of the row, opens up an overlay (modal view) to select various options. The overlay has an “OK” button which saves the selected option and calls composer.hideOverlay to close the Overlay.

The problem is my click on OK button works fine but it also ends up as a tap on the tableview (underneath on the parent scene) which again triggers opening of the overlay. This isn’t a problem when I tap in and about anywhere in overlay but only when I tap/click OK button which hides the overlay but also acts as a tap on the underlying tableview. I can’t seem to get around it. Is there a solution to this? 

Touching the screen in a tap fashion generates both tap and touch events.  Composer is supposed to catch those tap events as well as touch events.  Can you show the code leading up to and calling showOverlay()?

How would you call the function event.params.onSaveFn(data) from the overlay? Thanks

Perhaps:

event.params.onSave(data)

Where are you accessing the event.parent which function/event listener? Are you using Composer or Storyboard?

Ideally I would like to use event.parent somewhere later but just for test sake, I have it in function scene:create(event) of my overlay scene. I am using composer.

Thanks

In your parent scene, if you do:  scene.basketItemAttributes = {}

instead of:  basketItemAttributes = {}

Then in your overlay you can do event.parent.basketItemAttributes[whatever] to access the table entries.

Rob
 

You can also pass reference to your array to overlay using params option of showOverlay function.

composer.showOverlay("modifiers", {    params = {       basketItemAttributes = basketItemAttributes    } });

which you can access form overlay create function using event.params.basketItemAttributes

or even better you can pass function to overlay:

local function OnOverlaySave(data)    -- modify pizza and save end composer.showOverlay("modifiers", {    params = {       onSaveFn = OnOverlaySave    } });

in overlay you will just need to collect all data and call the function event.params.onSaveFn(data);

Thanks Rob and Hayk. Your solution worked like a charm.

I have one more small issue.

In the same example the parent scene has a tableview with a list of pizza’s and on a click of one of the row, opens up an overlay (modal view) to select various options. The overlay has an “OK” button which saves the selected option and calls composer.hideOverlay to close the Overlay.

The problem is my click on OK button works fine but it also ends up as a tap on the tableview (underneath on the parent scene) which again triggers opening of the overlay. This isn’t a problem when I tap in and about anywhere in overlay but only when I tap/click OK button which hides the overlay but also acts as a tap on the underlying tableview. I can’t seem to get around it. Is there a solution to this? 

Touching the screen in a tap fashion generates both tap and touch events.  Composer is supposed to catch those tap events as well as touch events.  Can you show the code leading up to and calling showOverlay()?

How would you call the function event.params.onSaveFn(data) from the overlay? Thanks

Perhaps:

event.params.onSave(data)