Announcement

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

    #16
    That's correct. As a convenience, we'll add $primaryDSRequest as a way to get to the original DSRequest for a cache sync operation, but it still seems like the simplest approach for you is to place the data on the servletRequest.

    Comment


      #17
      Well, it has been about 3 years, but I still haven't found a satisfactory solution to this. Just various workarounds depending on the situation, each of which has its drawbacks. Has anything been added in the interim that I may have missed.

      To get back to the original issue, what I need is some way to modify the add/update/remove requests generated by an editable ListGrid before they get sent to the server. DataSource.transformRequest() seems to be exactly what I'm looking for, but all of the references I see to it are in regards to RestDataSource.

      In my case I'm using the standard SQLDataSource on the server side and on the client side I've already loaded the DataSource in the standard way and use DataSource.get("Name") to get the ds object. Since I'm not creating a new DataSource I can't override the transformRequest method.

      Is there some other way to tap in to the transformRequest point for a standard DataSource, or even at the ListGrid level? Or should I create a new DataSource on the client side based on the existing datasource so I can override transformRequest? If that is the right way to do it, can you show me how to create a duplicate of the DataSource? Should I just use new DataSource(DataSource.get("Name").getJsObj())?

      Comment


        #18
        Look at the docs for transformRequest - for an already-created DataSource there is now an API to add a RequestTransformer which provides the same capability as transformRequest. It's been there a couple of years actually :)

        Comment


          #19
          Fantastic! How come nobody told me? :)

          This is exactly what I needed.
          Code:
          DataSource dataSource = DataSource.get("dsName", new RequestTransformer() {
          	
          	@Override
          	protected Object transformRequest(DSRequest dsRequest) {
          		// Modify dsRequest here
          		return getDefaultTransformRequest(dsRequest);
          	}
          }, new ResponseTransformer() {
          	
          	@Override
          	protected void transformResponse(DSResponse response, DSRequest request,
          			Object data) {
          		// Modify response here
          	}
          });

          Comment


            #20
            One last question. If I want to modify the values that are already in the DSRequest, how do I access them? dsRequest.getData() returns only a jsObj. Is there a way to get the data as a Record or Map?

            Comment


              #21
              I got it. Once way is to convert it back into a Record, modify the record and then setData with the modified record. In case anyone else comes upon this post, here is an example that works for me. I needed to add a value to the record before it is sent on an Add or Update request, and add a field to the criteria if it is a Fetch request.
              Code:
              DataSource dataSource = DataSource.get(IslandPacificDSConstants.DATASOURCE_Item, new RequestTransformer() {
              
              	@Override
              	protected Object transformRequest(DSRequest dsRequest) {
              		if (IPGui.isMultiBusinessFeatureEnabled()) {
              			if (dsRequest.getOperationType().compareTo(DSOperationType.ADD)==0 
              					|| dsRequest.getOperationType().compareTo(DSOperationType.UPDATE)==0) {
              				JavaScriptObject jsData = dsRequest.getData();
              				if (jsData!=null) {
              					Record rec = new Record(jsData);
              					rec.setAttribute(Item.ZONE, grid.getFilterStrip().getZone());
              					dsRequest.setData(rec);
              				}
              			} else if (dsRequest.getOperationType().compareTo(DSOperationType.FETCH)==0) {
              				dsRequest.getCriteria().addCriteria(Item.ZONE, grid.getFilterStrip().getZoneAsString());
              			}
              		}
              		return getDefaultTransformRequest(dsRequest);
              	}
              }, new ResponseTransformer() {
              
              	@Override
              	protected void transformResponse(DSResponse response, DSRequest request,
              			Object data) {
              		// Modify response here
              	}
              });
              One last question for Isomorphic. I promise this is the last one for this thread. :)

              DataSource.get() in this case still returns a singleton, correct? So once I use DataSource.get and pass it a RequestTransformer and a ResponseTransformer, does that mean that *all* subsequent client requests to that DataSource will be handled by those transformers, regardless of component, since there is only one instance of the DataSource client side, correct?

              Comment


                #22
                Yup that should be correct.
                You're modifying the DataSource, which doesn't care where the requests came from (whether initialized by a component or even via a direct API on the DataSource itself).

                Comment


                  #23
                  Thanks for confirming. Is there any way to clone the datasource to create a new Datasource object with transformRequest overriden and leave the original ds object unchanged?

                  Comment


                    #24
                    You broke your promise ;) In all seriousness, a new thread would be appreciated for further questions since we are three pages in to this thread and it's not obvious whether context from all previous posts is required to understand new ones..

                    No, there is no way to "clone" a DataSource. You can of course have some kind of global flag that causes your Request/ResponseTransformer to do nothing when you don't want it to, or to flip between two different behaviors.

                    You can also use DataSource.inheritsFrom to inherit fields, but by design this does not inherit other settings such as operationBindings.

                    Comment

                    Working...
                    X