Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

passing information through onRelease (newWidget.button)
Started by akuthia Aug 28 2014 12:56 PM

1 reply to this topic
button functions
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

akuthia

[GLOBAL: userInfoPane.html]
akuthia
  • Enthusiast

  • 53 posts
  • Corona SDK

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



[TOPIC: post.html]
#2

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

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.

 

local btnPurchase= widget.newButton {
   id = "com.mybundle.myapp.myproduct",
   shape = "roundedRect",
   ...
 
Then, in the listener function:
 
local function purchaseItem( event )
   local prod = event.target.id
   store.purchase( { prod } )
end
 
Hope this helps,
Brent



[topic_controls]
 
[/topic_controls]