admin管理员组文章数量:1336631
I'm learning Incisor and I'm trying to understand how to pass custom data into my callback function when a button is clicked. I have a block of JSON data and a button. I am using a pressCallback
and passing my data as a parameter. However, when I log the callback argument, I'm seeing a MouseEvent
object instead of the data I passed in.
Here is my sample code:
class ProjectMain {
init() {
let data = {"id": 0, "email": "[email protected]", "active": true};
this.button = new Button( nc.graphicAssets.WhiteBox, nc.mainScene, "MyButton" );
this.button.addPressCallback( this, "myPressCallback", data );
this.myPressCallback = function( data ) {
console.log('callback:', data );
}
}
}
In this code, when I click the button, the callback function is triggered, but the console logs a MouseEvent
object rather than the data
object I passed in. How can I properly pass and access the custom data
in the callback function?
I'm learning Incisor and I'm trying to understand how to pass custom data into my callback function when a button is clicked. I have a block of JSON data and a button. I am using a pressCallback
and passing my data as a parameter. However, when I log the callback argument, I'm seeing a MouseEvent
object instead of the data I passed in.
Here is my sample code:
class ProjectMain {
init() {
let data = {"id": 0, "email": "[email protected]", "active": true};
this.button = new Button( nc.graphicAssets.WhiteBox, nc.mainScene, "MyButton" );
this.button.addPressCallback( this, "myPressCallback", data );
this.myPressCallback = function( data ) {
console.log('callback:', data );
}
}
}
In this code, when I click the button, the callback function is triggered, but the console logs a MouseEvent
object rather than the data
object I passed in. How can I properly pass and access the custom data
in the callback function?
1 Answer
Reset to default 0To elaborate on this, as @James has mentioned, data
is already in scope and accessible, so you wouldn't need to pass it in
so both of these approaches would work
let data = {"id": 0, "email": "[email protected]", "active": true};
this.button.addPressCallback( this, "myPressCallback" );
this.myPressCallback = function() {
console.log('callback:', data );
}
let data = {"id": 0, "email": "[email protected]", "active": true};
this.button.addPressCallback( this, "myPressCallback", data );
this.myPressCallback = function( browser, camera, data_passed_in ) {
console.log('callback:', browser, camera, data_passed_in, data );
}
Here is the tutorial on buttons covered by Incisor:
https://www.youtube/watch?v=coR_HjYuchI
版权声明:本文标题:javascript - How can I pass custom data into an Incisor callback function with a button click? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742398771a2467458.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
data
should be in scope when myPressCallback runs without passing it. – James Commented Nov 19, 2024 at 21:46this.myPressCallback = function( browserEvent, camera, data ) {
- this pattern is covered in the addPressCallback docs for the third parameter. – James Commented Nov 20, 2024 at 3:36