Reputation: 12212
When the user comes to the site, an empty Shopping Cart is created on the client and server side. When he adds a product into the SC a RPC call is done to add the product on the server side. We need to do the same client side to be in sync and update the UI. If we don’t and switch to a view that has a reference to a old SC. The UI won’t be updated accordingly (empty SC whereas there is one product in it).
Solution 1
The RPC call returns the updated SC and do clientSC = returnedSC
. It’s not very efficient since it’s a lot of data to transfer over the wire.
Solution 2
The RPC call returns the added product and on the client side, call a method addProduct
that add the product to a list instead of storing it in the datastore (like on the server side).
Solution 3
Same as solution 2 but use an event to notify the presenters (I use the MVP pattern) that have a reference to a SC.
Which one is the best, more generally what is the best practice to keep in sync the client and server model?
Upvotes: 3
Views: 1557
Reputation: 3013
Use RequestFactory, which comes along with gwt 2.1.
http://code.google.com/webtoolkit/doc/latest/DevGuideRequestFactory.html
RequestFactory makes it easy to implement a data access layer on both client and server. It allows you to structure your server-side code in a data-centric way and provides a higher level of abstraction than GWT-RPC, which is service-oriented rather than data-oriented. On the client side, RequestFactory keeps track of objects that have been modified and sends only changes to the server, which results in very lightweight network payloads. In addition, RequestFactory provides a solid foundation for automatic batching and caching of requests in the future.
RequestFactory uses its own servlet, RequestFactoryServlet, and implements its own protocol for data exchange between client and server. It is not designed for general purpose services like GWT-RPC, but rather for data-oriented services as we will see shortly. RequestFactory does not use GWT-RPC and is not intended to replace it. It is designed specifically for implementing a persistence layer on both client and server.
Upvotes: 4