Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Custom attributes in DSResponse. Is it possible?

    Hi, could anybody help me with the following?
    I am trying to insert my custom attributes (tl_page, etc.) into DSResponse object like that

    {
    "response":{
    "status":0,
    "startRow":0,
    "endRow":49,
    "totalRows":50,
    "tl_page":1,
    "tl_totalRows":500,
    "tl_totalPages":50,
    "data":[... etc.

    but in the DSCallback in the "execute" method the list of attributes (DSResponse.getAttributes()) does not contain these my attributes. Is there any way to get these attributes from DSResponse? Or maybe from "rawData"?

    Thank you.

    #2
    The answer is in DataSource.transformResponse(DSResponse response, DSRequest request, Object data) and in the Object data in particular. This very "data" in a JSON case is a JavaScriptObject and you can get any custom attribute from it using JSOHelper class and put it manually in "response"
    Last edited by asg2007; 28 Nov 2010, 06:44.

    Comment


      #3
      Code:
      protected void transformResponse(DSResponse response, DSRequest request, Object data) {
        super.transformResponse(response, request, data);
        JavaScriptObject resp = JSOHelper.getAttributeAsJavaScriptObject((JavaScriptObject) data, "response");
        String tl_page = JSOHelper.getAttribute((JavaScriptObject) resp, "tl_page");
      }
      I am doing it like in the code above, but why doesn't data come as a different object so that we have easier access to custom attributes?
      The attributes does not even have to come under "response" attribute, it could come directly at the same level as the "response" tag. Or there should be maybe a "customResponse" attribute that would be available from DSResponse which would give a map of values found in "{response:{....},customResponse{custom attributes here}}" so one would not mix already existing smartgwt tags in the response tag.
      Are there performance concerns or something?

      Comment


        #4
        It looks like in 2.4 we lost this ability to get custom attributes this way.
        I used the code : if (JSOHelper.isJSO(data)){
        JavaScriptObject rawResponse = (JavaScriptObject)data;
        Map rawResponseMap = JSOHelper.convertToMap(rawResponse);
        Map rawResponseAttributesAsMap = JSOHelper.convertToMap( ((JavaScriptObject)rawResponseMap.get("response")) );
        }
        but now I can not see my custom attributes in rawResponseAttributesAsMap.
        That's pitty.

        Comment


          #5
          On the server, I just use DSResponse.setAttribute(key,value). As long as there is not name conflict, this seems good enough.

          The other suggestion is to add it as extra fields to a record, which is fine if your response will always contain data. Then you use Record.setAttribute(key,value).

          Comment


            #6
            Originally posted by asg2007
            It looks like in 2.4 we lost this ability to get custom attributes this way.
            Following your attempts, I've found that the following snippet works on SmartGWT 2.4
            Code:
            @Override
            protected void transformResponse (DSResponse response, DSRequest request, Object data) {
                super.transformResponse (response, request, data);
            
                JavaScriptObject resp = JSOHelper.getAttributeAsJavaScriptObject ((JavaScriptObject)data, "response");
                String mycustomattr = JSOHelper.getAttribute (resp, "mycustomattr");
                ...
            }
            Tested on SmartClient Version: SC_SNAPSHOT-2010-12-31/LGPL

            Cheers
            Davide

            Comment


              #7
              Thank you, Davide. It was helpfull and timely.

              Comment

              Working...
              X