passing information through onRelease (newWidget.button)

Hello everyone,

I’m working on my store at the moment, and found a pared down store in one of the threads that I’m adapting. however, I want to use one function for actually buying any item that’s sold, by passing the information from the purchase button. The button itself looks like this:

local btnPurchase= widget.newButton { shape="roundedRect", width = 200, height = 40, cornerRadius = 2, fillColor = { default={ 1, 0, 0 }, over={ 1, 0.1, 0.7 } }, strokeColor = { default={ 1, 0.4, 0 }, over={ 0.8, 0.8, 1} }, strokeWidth = 4, onRelease = purchaseItem, label = "Purchase" }

what I’d like to do, is pass the item, that is currently hardcoded into the purchase function

local function purchaseItem(event,product) --make sure you add { } around your product id as you need to send a table value... not a string! store.purchase( {"com.mybundle.myapp.myproduct"}) end

into the onRelease on line 48. Any way to do it, or is the best thing to just to write my own listeners? TIA

Hi @akuthia,

The best way is to include an “id” parameter (string) in the button constructor which identifies the button. Then, in the onRelease function, just detect “event.target.id” and that should equal the same id parameter you set.

[lua]

local btnPurchase= widget.newButton {

   id = “com.mybundle.myapp.myproduct”,

   shape = “roundedRect”,

   …

[/lua]

Then, in the listener function:

[lua]

local function purchaseItem( event )

   local prod = event.target.id

   store.purchase( { prod } )

end

[/lua]

Hope this helps,

Brent

Hi @akuthia,

The best way is to include an “id” parameter (string) in the button constructor which identifies the button. Then, in the onRelease function, just detect “event.target.id” and that should equal the same id parameter you set.

[lua]

local btnPurchase= widget.newButton {

   id = “com.mybundle.myapp.myproduct”,

   shape = “roundedRect”,

   …

[/lua]

Then, in the listener function:

[lua]

local function purchaseItem( event )

   local prod = event.target.id

   store.purchase( { prod } )

end

[/lua]

Hope this helps,

Brent