Announcement
Collapse
No announcement yet.
X
-
Generally, send the data for the updated record via messaging and form a DSResponse from that data client side. Don't do a separate fetch.
-
I tried using DataSource.updateCaches() before, but it doesn't do enough for me. I.e. if I use the DataSource to "fetch" the updated record from the database (because I heard via RTMM that the record was changed by someone else), then I get a response back which has a DSRequest/DSResponse. However, that DSResponse is for a "fetch", and not an "add" or "update". So, when I say DS.updateCaches() with that DSResponse, the observing ListGrid doesn't get notified of the change.
My understanding is that the caches would get updated only if I had done an "add" or "update" (I think I read this in the docs somewhere). If I do the fetch and then immediately do a DS.update(recordIjustFetched), then the observing ListGrid does get notified of the change. I don't think that doing a "update" on a record that I just read from the DB is a good idea, because the record could have been changed by someone else between the time I fetched and when I update.
How do you suggest handling this?
Thank you!
Leave a comment:
-
On broadcasting changes, see the DataSource.updateCaches() API. As far as type safety, it's an intentional choice to have only field-level type safety - see explanation above.
Leave a comment:
-
Thanks for the info. Here's what's I'm up to...
It's sometimes a regarded principle to have stronger type safety by using well-defined objects rather than putting everything into a somewhat un-typed array. I.e. the SmartGWT code base nicely makes specific strongly-typed enum values, instead of just making everything an int.
For stronger type safety, I was initially thinking I'd make a "Response" object for responding to an RPC call (i.e. something I attempted to process failed, so here's the "false" return status, plus here's the "reason" why it failed, as well as some further "info" related to the reason). For example, {status:false, reason:"exception", info:"NullPointerException"}. This can be done as a map (which is what I've implemented for now), but was wondering if it can be an actual object.
I'm also now using the Real-Time Messaging Module. If I notice on the server side that a business entity has changed (i.e. a "Bicycle" record in my DB has changed state), then it'd be nice to load the "Bicycle" record from the DB *once* on the server and to tell anyone who's listening "Hey, this Bicycle has changed, and here is the new record"...Instead of having each individual client then hit the database separately to load its own copy of the Record.
Do you have a suggestion for how to handle broadcasting changed objects to clients?
A further description of my scenario is that I have an "audit" table that gets updated whenever any row in the DB changes (whether it's a change made through the SmartServer or even a manual/SQL change to the DB). I'd like to setup a back-end thread to monitor this table and relevant changes to listening clients ("Hey, this Bicycle has changed!").
Thoughts for how to best accomplish this with SmartServer/SmartGWT?
Thank you!
Leave a comment:
-
Generally data is translated to "typeless" data such as Maps or Records. Only the field values retain their (atomic) types such as Date. This is the best practice for the vast majority of use cases, so that you avoid having to define redundant DTO classes to avoidw serialization issues with your data.
If you're interested in re-creating a specific bean class on the client, please explain the overall use case and we can suggest the best approach for re-creating a bean in that scenario, or a better alternative approach.
Leave a comment:
-
Ok, so I found a way to send a LinkedHashMap in the RPCResponse from the server to the client, and to re-constitute it back into a usable Map on the client. It uses the JSOHelper to convert the JavaScriptObject back to a usable Java object:
// Client code that initiates the RPC request and hears the response:
// Server (servlet) code that sends the response back, raw data containing a map:Code:RPCManager.sendRequest(rpcRequest, new RPCCallback() { @Override public void execute(RPCResponse response, Object rawData, RPCRequest request) { RPCActionResponse rpcActionResponse = null; try { JavaScriptObject jso = (JavaScriptObject)rawData; Map<String,String> map = (Map<String,String>)JSOHelper.convertToMap(jso); // Do something with our Java Map! String msg = map.get("MyMessage"); // msg should now contain "Hello, world!" } catch(Exception ex) { // Log this...didn't get our map?!? } } });
I think this is pretty much what I have working.Code:protected void doPost(HttpServletRequest request, HttpServletResponse response) { RPCManager rpc = new RPCManager(request, response); LinkedHashMap<String,String> map = new LinkedHashMap<String,String>(); map.put("MyMessage", "Hello, world!"); rpc.send(map); }
Is there a "best practice" for serializing an object and sending it? I.e. a bean?
Leave a comment:
-
Send object or map via RPCResponse data? (SmartGWT Pro)
Hi,
I'm trying to send a non-trivial object back from an RPCRequestServlet. I can pass back a string (and cast it to a String on the client side), but can't seem to send a simple class or a map. I really just want to pass back data in a more organized way than munging it all together in a string.
I've tried an object, Serializable object, a LinkedHashMap, but with no luck. I can pass a LinkedHashMap from the client to the server in a RPCRequest, and it gets nicely casted to a LinkedMap. But I'm not sure how to go the other way.
The server docs indicate that I should be able to pass back pretty much any old object, as long as it's fairly primitive types, arrays, a map, etc.
Can anyone share an example of what exact type they instantiate and pass as the "data" in the RPCResponse on the server, and then how they re-constitute it into a usable object on the server side (i.e. exact type they cast it to on the server)?
Thanks much!Tags: None
Leave a comment: