salesforce的response.getReturnValue()

这些日子在搞salesforce,Lightning语言开发,做一些例子。有些没弄懂的东西,在这儿记录一下。

参考:https://www.cnblogs.com/zero-zyq/p/10489626.html

https://trailhead.salesforce.com/en/content/learn/superbadges/superbadge_lcf

 

1,response.getReturnValue()

这首先是一个salesforce Lightning组件的一部分。

BoatSearchResultsHelper.js的代码是这样的:

我大概解释一下这些都是什么鬼哈。

 

onSearch:方法名,别人调用的时候,一般用helper.onSearch来调用它。

 component:就是salesforce里的组件容器啦,比如这里的component.get("v.boatTypeId1") 。这个boatTypeId1就是组件名。前面那个v就是说这是个变量,在salesforce组件的视图里,定义的变量都可以用v来引用,跟Javascript的万金油document.getElementTypeById差不多。

     为了方便理解,我贴俩变量:

     <aura:attribute name="boats" type="Boat__c[]"  />

     <aura:attribute name="boatTypeId1" type="String" />

 

c.getBoats:这个玩意儿就是服务器端的东西。称之为Apex。用它来调回服务器代码,返回数据库数据用的。

 完整的请看下面的源码。c嘛,就是说是服务端controller。题外话,由于salesforce是客户端和服务器端都有controller,所以名字别重了,不然出错的时候,挺难理解的。

 

action.setParams:这个就是传参数啦。

 

action.setCallback:这个是回调。因为salesforce是多租户环境,所以呢都是异步,不会等你执行完的。那怎么知道执行完了呢,就是使用这个回调,服务端执行完了,就返回来执行它,继续干活。

 

说了一大堆,我的问题终于到了:

component.set("v.boats",response.getReturnValue());这个是返回服务器端数据用的。

boats是一个视图组件变量,类型是Boat__c[ ]。这个东西按理来说,应该是一个Jason对象,里面嵌一堆具体的数据。

比如像

{

        {

             "BoatType__c":aaaaaaa1,

             "Description__c":bbbbbb,

             .....

        },

        {

             "BoatType__c":aaaaaaa2,

             "Description__c":bbbbbb,

             .....

        },

        ......

 }

 

但是当我在Chrome按F12,Debug底下看变量的时候,它居然给我以下这个玩意儿:

value: function(){const o=$r(e,arguments,r);let i=t[n](...o);return r.afterCallback&&(i=r.afterCallback(i)),r.metrics&&Qt(n),Fr(e,i,r)}

 

有谁能告诉我,这个东西就是是怎么看的吗??先谢谢了。

 

<源码参照>

({

    onSearch : function(component,event) {

        var currentBoatType = component.get("v.boatTypeId1");

        var action = component.get("c.getBoats");

        if(currentBoatType=='All Types'){

            currentBoatType = '';

        }

        action.setParams({

            "boatTypeId":currentBoatType

        });

 

        action.setCallback(this,function(response){

            var state = response.getState();

            if(component.isValid() && state ==='SUCCESS'){

                component.set("v.boats",response.getReturnValue());

            }else{

                console.log("Failed with state1:"+state);

            }

        });

        $A.enqueueAction(action);

    }

})

 

<Apex参照>

public class BoatSearchResults {

    public List<Boat__c> Boats{get;set;}

 

    @AuraEnabled

    public static List<BoatType__c> getboattypes(){

        return [select Name,Id From BoatType__c];

    }

 

    @AuraEnabled

    public static List<Boat__c> getBoats(string boatTypeId){

        List<Boat__c> boatTypeList = new List<Boat__c>();

        if(boatTypeId!=''){

            boatTypeList=[

                select 

                    Id,

                    BoatType__c,

                    Picture__c,

                    Name,

                    Contact__r.Name,

                    Geolocation__Latitude__s,

                    Geolocation__Longitude__s

                from Boat__c

                where BoatType__c = :boatTypeId

            ];

        }else{

            boatTypeList=[

                select 

                    Id,

                    BoatType__c,

                    Picture__c,

                    Name,

                    Contact__r.Name,

                    Geolocation__Latitude__s,

                    Geolocation__Longitude__s

                from Boat__c

            ];

        }

        return boatTypeList;

    }

 

    public BoatSearchResults() {

 

    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章