我可以将两个参数传递给 Worklight 适配器的 onSuccess 函数吗?

Can I pass two arguments to a Worklight adapter onSuccess function?

我无法使用 IBM Worklight 将两个参数传递给适配器 onSuccess() 函数。请给我指路。这是我目前正在尝试的:

var options = {
    onSuccess : SubCategoriesSuccess(options, result),
    onFailure : SubCategoriesFailure,
    invocationContext: {}
};

onSuccess 参数需要对函数的 引用 ,而不是对函数的调用 - 请注意 SubCategoriesSuccessSubCategoriesSuccess() 在 JavaScript 中。您正在做的是传递调用 SubCategoriesSuccess(options, result).

的结果

您需要的是编程术语中通常所说的部分调用。 JavaScript 本身具有执行此操作的功能 - Function.prototype.bind()。您可能应该看看那个(尽管各种 JavaScript 工具包也提供了替代方案)。

这意味着您的代码将类似于:

{
  onSuccess : SubCategoriesSuccess.bind(this, options, result),
  onFailure : SubCategoriesFailure,
  invocationContext: {}
};

请注意,我没有测试过这个。

如果选项是您的调用函数中已有的变量。您必须将 return 函数包装在另一个函数中。所以换句话说,成功是从适配器 return 得到的,而选项只是由调用函数传递。

 onSuccess:function(result){
    SubCategoriesSuccess(options, result);
 }