Lightning Component中使用Promise处理Apex服务端请求
- 1月
- 06
- Posted by Lin
- Posted in APEX, Lightnint Component, Salesforce
In Lightning Components, each communication with Apex Server-Side is handled as a Javascript async request, and the callback you set is called after the server-side action is completed. A server-side action can return any JSON object, such as sObject or a map of name-value pairs.
The followoing sample code shows how a server-call works in a Lightning Component.
More about Javascript Promise you can reference here:
https://developers.google.com/web/fundamentals/primers/promises
Enjoy it!
The followoing sample code shows how a server-call works in a Lightning Component.
SampleAura.cmp
SampleAuraController.js
SampleAuraController.cls
If there have multiple requests, you need to define multiple actions and callback functions for them, then the source code may looks like below :SampleAuraController.js
Using Promise in Javascript is a way to avoid this “Callback Hell“, and fortunately, Promise can be used in Lightning Compoenent, as below:SampleAuraController.js
SampleAuraHelper.js
Then the source code looks more clean and more elegant, isn’t it?More about Javascript Promise you can reference here:
https://developers.google.com/web/fundamentals/primers/promises
Enjoy it!
近期文章
- Salesforce DevTools Release Notes (version 3.10.0) – mass edit custom fields on lightning
- Salesforce DevTools Release Notes (version 3.9.0)- Object Fields, SOQL Query Editor
- Salesforce DevTools Release Notes (version 3.8.0)
- Salesforce DevTools的开发者用户突破10,000名!THANK YOU!
- Lightning Component中使用Promise处理Apex服务端请求
Top Posts
- Salesforce DevTools – Saleforce开发者必备Chrome扩展 - 228.9K views
- Date format 与 DateTime format - 211.3K views
- Salesforce批量编辑 (Chrome扩展) – 在任何标准视图上进行批量插入, 拷贝, 更新, 删除, 支持Classic与Lightning! - 161.9K views
- 编写一个可被计划执行的Batch Apex - 126.7K views
- 用Sublime Text 3架设Salesforce的快速开发环境 - 123.3K views
分类目录
- APEX
- 应用
- Custom Label
- Custom Metadata
- Custom Setting
- Design & UI
- 交流会
- Heroku
- iOS & Object-c
- Lightnint Component
- Linux & Mac OS
- Metadata API
- Node.js
- Open Source
- Other
- PHP Framework
- Release Notes
- REST API
- Salesforce
- Salesforce DevTools
- Salesforce DX
- SOQL
- Tooling API
- Unit Tests
- Validation Rule
- Visualforce
- WordPress
您必须登录才能发表评论。