首页 技术 正文
技术 2022年11月12日
0 收藏 739 点赞 2,554 浏览 5011 个字

我们做的项目好多都是多语言的项目,针对不同国家需要展示不同的语言的标题。我们在classic中的VF page可谓是得心应手,因为系统中已经封装好了我们可以直接在VF获取label/api name等方法。但是我们在lightning aura中开发却发现这个常用的功能并没有包含,好吧,既然没有现成可用的那我们就要有workaround的方式去后台获取。此篇主要封装好组件去实现获取某个object或者某些object相关字段的label。

那我们来开始进行这个组件的开发,开发以前我们需要先思考一下,组件化的东西,传参应该是什么,返回应该是什么,应该实现哪些功能解决哪些痛点。如何用到更好的优化。本人思考可能并不特别的完全,感兴趣的可以进行优化。

1. object 的API name应该为必填项。 这里应该实现可以同时获取多个表的字段的label信息,我们画component时,很可能需要获取当前的对象,父对象以及相关的子对象的字段的label,所以此处传参应该能做到传递list而不是单一的object

2. object对应的指定的field的api name列表,此项应该为可选项,非必填。我们都知道aura开发现在很慢,而且我们在前台获取label时,可能一个object有上百个字段,但是我们在页面只需要某几个字段的label的信息,如果全部查出来放在前台特别影响view state,所以我们此处应该支持可以通过指定的一些字段进行查询。因为object传参是list,所以此参数应该为Map<String,List<String>>方式。

3. 返回类型应该为 Map<String,Map<String,String>>类型,外层的key是objectAPIName,内层的map的key是fieldAPIName,内层的map的value为我们需要的field label

OK,上面的已经梳理出来,那干就完了。

一. 公用组件搭建

FieldLabelServiceController.cls 用于后台搭建查询指定的obj / field的value -> label信息

 public with sharing class FieldLabelServiceController {
/*
* @param objApiNameList : object API name list. eg:['Account','Contact']
* @param objApiName2FieldsMap: object API name 2 fields map. eg:{'Account':['Name','Type'],'Contact':['LastName','Phone']}
* @return object API name 2 map of field API name -> label name. eg:{'Account':{'Type':'类型'},'Contact':{'LastName':'姓'}}
*/
@AuraEnabled
public static Map<String,Map<String,String>> getFieldLabelService(List<String> objApiNameList,Map<String,List<String>> objApiName2FieldsMap) {
// key: object API name ; value : (Map: key:field API name, value: field label)
Map<String,Map<String,String>> object2FieldLabelMap = new Map<String,Map<String,String>>();
//get all sobject sObjectType map
Map<String,sObjectType> objName2ObjTypeMap = Schema.getGlobalDescribe();
for(String objApiName : objApiNameList) { //1. get specific object sObjectType
sObjectType objType = objName2ObjTypeMap.get(objApiName);
//2. get all of the fields map via specific object
Map<String,Schema.SObjectField> fieldsMap = objType.getDescribe().fields.getMap(); //3. check if retrieve specific field list or all the fields mapping via object
Set<String> retrieveFieldList = new Set<String>();
if(objApiName2FieldsMap != null && objApiName2FieldsMap.containsKey(objApiName)) {
retrieveFieldList = new Set<String>(objApiName2FieldsMap.get(objApiName));
} Map<String,String> fieldApiName2FieldLabelMap = new Map<String,String>();
//4. get all / specific field api name -> label name mapping
for(String fieldApiName : fieldsMap.keySet()){
if(retrieveFieldList.size() > 0 && !retrieveFieldList.contains(String.valueOf(fieldsMap.get(fieldApiName)))) { continue;
} String label = fieldsMap.get(fieldApiName).getDescribe().getLabel();
fieldApiName2FieldLabelMap.put(String.valueOf(fieldsMap.get(fieldApiName)), label == null ? fieldApiName : label);
} object2FieldLabelMap.put(objApiName, fieldApiName2FieldLabelMap);
}
return object2FieldLabelMap;
}
}

FieldLabelService.cmp:用于封装共用方法

 <aura:component access="global" description="Field label service" controller="FieldLabelServiceController">
<aura:method access="global" name="getFieldLabel" action="{!c.getFieldLabelAction}">
<aura:attribute type="List" name="objectAPINameList" required="true" description="object list to retrieve field label" />
<aura:attribute type="Map" name="objectFieldAPINameMap" description="specific fields need to retrieve via object api name"/>
<aura:attribute type="Function" name="callback" required="true" />
</aura:method>
</aura:component>

FieldLabelServiceController.js:用于封装对应的controller js方法,调用后台获取结果

 ({
getFieldLabelAction : function(component, event, helper) {
const params = event.getParam('arguments');
const action = component.get('c.getFieldLabelService');
action.setParams({
"objApiNameList" : params.objectAPINameList,
"objApiName2FieldsMap":params.objectFieldAPINameMap
});
action.setCallback(this, function(response) {
const state = response.getState();
if (state === 'SUCCESS') {
params.callback(response.getReturnValue());
} else if (state === 'ERROR') {
const errors = response.getError();
if (errors) {
console.error(JSON.stringify(errors));
} else {
console.error('Unknown error');
}
}
}); $A.enqueueAction(action);
}
})

至此组件封装完成,下面是调用部分。调用部分没有UI,感兴趣的自行画UI。

二. 公用组件测试

FieldLabelTestComponent:用于引入公用组件,并且初始化获取Account/Contact的field label。

<aura:component implements="flexipage:availableForAllPageTypes">
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="accountFieldLabelMap" type="Map"/>
<aura:attribute name="contactFieldLabelMap" type="Map"/>
<c:FieldLabelService aura:id="service"/>
</aura:component>

FieldLabelTestComponentController.js:用于后台调用公用组件的方法,传值,针对response进行解析获取自己需要的内容。demo中针对account只获取name以及type的值,对contact获取所有字段的label值。

 ({
doInit : function(component, event, helper) {
const service = component.find('service');
let objectAPINameList = ['Account','Contact'];
let objectFieldAPINameMap = {'Account':['Name','Type']};
service.getFieldLabel(objectAPINameList,objectFieldAPINameMap,function(result) {
console.log(JSON.stringify(result));
component.set('v.accountFieldLabelMap',result['Account']);
component.set('v.contactFieldLabelMap',result['Contact']);
console.log(JSON.stringify(result['Account']));
console.log(JSON.stringify(result.Account));
});
}
})

结果展示:针对account只获取了指定的字段的label,Contact获取了所有的label信息。可以使用[]方式或者.的方式获取详细内容。

salesforce lightning零基础学习(十六) 公用组件之 获取字段label信息

总结:篇中简单的介绍了针对aura情况下获取field label的公用组件的实现。篇中有错误的地方欢迎指出,有不懂的欢迎留言,有可以优化的地方欢迎交流并且鼓励优化。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,994
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,507
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,350
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,135
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,768
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,845