GWT - Should the server return the raw data or the widget?
Newbie question:
As a response to a request, my server returns a Tree-like data structure , and I want to present it as a tree at the client. However , the logic of transforming it into a tree 开发者_运维知识库widget is a bit complex.
Should I build the GWT Tree widget at the server and just return it as-is , or should I stick with raw data , and formulate the widget at the client?
(1) You want to transfer pure data (Java objects) between the server and the client. The client (in-browser code) should be responsible for turning pure data into a visual representation for the user.
(2) In addition, because GWT widgets reference parts of the browser DOM, I'm pretty certain you couldn't instantiate those objects on the server anyway.
Two reasons why I think you shouldn't send the whole widget tree:
- To pass the processing to the clients makes your servers life much easier especially when dealing with a lot of expensive requests. That's what GWT is all about: let the client do the work.
- It is not the responsibility of the server to do UI related stuff. Features like deferred binding (i.e., deciding what parts of your app are needed for the specific browser the request comes from) are done on the client after the
no.cache.js
has been loaded. This can't be done on the server.
Maybe you need a data structure that contains preprocessed layout data? You can preprocess the layout code you need on the client side, stick it into an Array/ArrayList of some kind of special classes. Maybe, you can then bypass the client side logic of building the tree and just iterate over the list? But you should check the timings, e.g. for sorting a big list it is faster to sort on the server and send back the list, for a small list the networking overhead is bigger than the slower browser javascript. I don't know if that holds true for your example.
精彩评论