在闪电中执行单个服务器调用而不是多个调用

Perform a single server call in lightning instead of multiple calls

在助手中,我有一些方法,每个方法都在组件控制器中调用一个 @AuraEnabled 方法。

其中一些电话仅在 'init' 活动期间。 从性能的角度来看,我应该在 'init'.

期间只调用一次

实现此目的的优雅方法是什么?

在 'init' return 字符串列表、小数和字符串期间调用的方法。

在您的控制器中定义一个 custom Apex class,它封装了您希望从 init 事件的单个调用中获取的所有信息:

public class InitializationWrapper {
    @AuraEnabled 
    public List<String> myStringList {get; set;}
    @AuraEnabled 
    public Decimal myDecimal {get; set;}
    @AuraEnabled 
    public String myString {get; set;}
}

return 这个包装器的一个实例 class 从你的 server-side Apex 调用到你的 init 处理程序。你只需要制作一个 round-trip.

我发誓你不需要做任何事情,不需要花哨的编码,SF 会作为 Aura 框架的一部分为你做...

前阵子曾经对我很有帮助(事实上,甚至相反,肯定是 1 个 Apex 调用 = 我使用的所有方法都使用相同的调控器限制,我不得不重新处理一些查询)

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/controllers_server_actions_call.htm

$A.enqueueAction(action) adds the server-side controller action to the queue of actions to be executed. All actions that are enqueued will run at the end of the event loop. Rather than sending a separate request for each individual action, the framework processes the event chain and batches the actions in the queue into one request. (...) The framework batches the actions in the queue into one server request. The request payload includes all of the actions and their data serialized into JSON. The request payload limit is 4 MB.

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/controllers_server_actions_queue.htm

The framework queues up actions before sending them to the server. This mechanism is largely transparent to you when you’re writing code but it enables the framework to minimize network traffic by batching multiple actions into one request (XHR). The batching of actions is also known as boxcar’ing, similar to a train that couples boxcars together.

The framework uses a stack to keep track of the actions to send to the server. When the browser finishes processing events and JavaScript on the client, the enqueued actions on the stack are sent to the server in a batch.

如果这不能像您描述的那样工作(您可以 post 一些代码吗?)并且请求大小小于 4 MB...也许他们破坏了某些东西并且您发现了平台错误。您确定在调试日志中看到单独的条目吗?还是在浏览器的网络流量监控中?

也许你需要玩玩 background actions。我的意思是这应该在 Apex 中无需额外的拐杖即可工作,将多个调用捆绑为一个,创建复杂的响应包装器 class 并且每个回调只取消它关心的数据,这是很多不必要的代码:/