Announcement

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

    Setting properties to request

    Hi,

    I am using SmartGWT version 3.0 and would like to send additional properties in request body paylod message sent to server in AJAX communcation, I have following scenario:

    1. Create custom DataSource that extends from com.smartgwt.client.data.DataSource
    and in constructor have
    Code:
    setDataProtocol(DSProtocol.CLIENTCUSTOM);
    setDataFormat(DSDataFormat.CUSTOM);
    setClientOnly(false);
    setCacheAllData(true);
    2. ListGrid that has set created dataSource in point 1 and has defined fetch method:
    Code:
    @Override
    public void fetchData(Criteria criteria, final DSCallback responseCallback, DSRequest request) {
    if (request == null) request = new DSRequest();
    request.setAttribute("myProp", "hello");
    Map<String, String> params = new HashMap<String, String>();
    params.put("myProp2", "hello2");
    request.setParams(params);
    super.fetchData(criteria, responseCallback, request);
    }
    3. in transformRequest I would like to get these request properties and use it in construction of request, but there it another instance of DSRequest object that does not have properties I set up before:
    Code:
    @Override
    protected Object transformRequest(DSRequest request) {
    String  myProp = request.getAttribute("myProp");
    assert myProp != null;
    }
    myProp property and also set parms have null values there, I tried to get it in many ways but without sucess

    Problem is caused by setting
    Code:
    setCacheAllData(true);
    , when I remove this setter, request has my parameter but with setting it to true SmartGWT creates new DSRequest instance with other properties.

    Could you help me how to achieve my goal?

    thanks,

    Michal

    #2
    With cacheAllData true that is some inherent ambiguity about which parts of the dsRequest are intended to be used only for requests to the server or also for the (in-browser) requests that subsequently happen against the cache.

    The best approach is probably to find some other way to get access to these params, rather than relying on putting them in dsRequest properties.

    Comment


      #3
      thanks for answer,

      Do not you think that it is bug in SmartGWT? I think that SmartGWT when constructs new DSRequest it should copy all attributes from former request object.

      What is recommended approach to send custom properties in body request to server?
      Only one solution I see there that widgets will hold it and datosource will request it from widget but that is not good solution from design point of view - widget should not care for that.

      thanks,

      Michal

      Comment


        #4
        No, not a bug - see above - of you are going to use cacheAllData use a less ambiguous method to affect requests and responses.
        Last edited by Isomorphic; 25 Apr 2012, 00:07.

        Comment


          #5
          sorry, but I do not understand what you mean by "less ambiguous method to affect requests"

          I have following scenario:
          1. I want to sent custom properties in message body to server, because server needs it and is not able to answer without it
          2. I want to cache received records on client

          I think these requirements are quite common in a lot of projects but I am not able to achieve them because when I set cache data to true I am not able to set properties to request.

          so my question is - is there any solution how to set properties to request when calling fetchData?

          Datasource is singleton instance for all widgets so datasource is not responsible for setting some specific properties to request for server when it does not receive it but it never receive it because all properties I set there are dropped by SmartGWT javascript.

          thanks.

          Comment


            #6
            Wait, can you clarify which is going on:

            1. you are seeing your params go to the server, but you are not seeing them in transformRequest for subsequent requests *which do not go to the server*

            OR

            2. as soon as you set cacheAllData:true your params never go to the server

            Comment


              #7
              when I set cacheAllData to true and before calling fetchData have code:
              Code:
              Map<String, String> params = new HashMap<String, String>();
              params.put("myParam", "HELLO");
              request.setAttribute("myProperty", "HELLO2");
              request.setParams(params);
              in transformRequest there are no params and no attribute with name "myProperty" and also they are not sent to server.

              in summary, I am not able to send custom properties in server request

              thanks

              Comment


                #8
                Ah, we thought you were talking about the reverse effect.

                This is intentional - the dsRequest that is send to the server to retrieve all data *is not the same and must not be the same* as the dsRequest that triggers this fetch. The special request to retrieve all data is stripped of all criteria, row ranges, etc, including params, which are assumed to possibly be criteria or in some way limit the server's response.

                If there's something you need to add to requests that go to the server you need to add it in transformRequest or make it part of the dataURL.

                Comment


                  #9
                  transformRequest is method of DataSource that is shared (the same instance) with multiple widgets that handles instances of the same entity. So DataSource must not be responsible for adding some extra parameters to the request that source of request (widget) wants to have.

                  If widget wants to send some extra parameters that are not part of criteria, the only way is to set some property with request scope to DataSource and datasource will use it in transformRequest? is not it there some other way?

                  thanks

                  Comment


                    #10
                    Again, consider the special requirements of cacheAllData mode. The DataSource must be able to issue a request that gets all data that the DataSource could ever return.

                    If components are adding dynamic parameters to the dsRequest that would affect the server's response, this won't work with cacheAllData mode, because these parameters are going to have no effect when fetching from cache.

                    If the parameters are not specific to a particular request and should be present in all requests, they can go into the dataURL or be added via transformRequest.

                    Comment

                    Working...
                    X