简体   繁体   中英

js - pass extra param to callback function

I'm trying to pass an extra param to a 3rd callback function (its not my own callback).

const selectedItems = dt.rows({ selected: true });
const selectedIndexes = selectedItems.indexes();
const selectedData = selectedItems.data();

let data_object = [];

$.each(selectedData, (i, o) => {
  data_object.push(o['PackageName']);
});

window.bridge.deleteApps(data_object, (success_list, selectedIndexes) => {
  console.log("test"); // selectedIndexes -> undefined

});

Background: It's a function that comes with Qt that triggers a python method (pyqt), the first param is passed to python the second param is a callback function with the return from the python method ( success_list ) but i need selectedIndexes too.

When I do

window.bridge.deleteApps(data_object, (success_list, abc=selectedIndexes) => {
  console.log("test"); // abc + selectedIndexes is available

});

I'm sorry that I've no working snippet for you to test but I did some researchs about callbacks and actually don't understand it, so I'm not able to reproduce this case.

I think part of your confusion is how callbacks are handled. Basically it's up to the window.bridge.deleteApps function to pass parameters to the callback you provide. So unless you're the author of that function, there's not a good way to have it pass you additional parameters. However, in the above example you should have access to selectedIndexes because you've declared it with const and it will be accessible from your callback.

So you should be able to have this code:

window.bridge.deleteApps(data_object, (success_list) => {
  console.log(selectedIndexes); // Should be available because you've declared it in a higher scope
  console.log(success_list); // Gets passed by the .deleteApss function
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM