Wijsmullerbros.nl

I know, I know the sun is hot, Mosquitos come suck your blood
Leave you there all alone, Just skin and bone

— Josh Homme

Oct
8

In my last post I talked a little about how the host page can supply data for the client application.

Now I would like to show in some more detail how to use the AutoBean system for this purpose.

The MyInitDataFactory is my custom arbitrary marker interface that extends the AutoBeanFactory interface which is responsible for the AutoBean magic.

The MyInitData class implements my simple custom IMyInitData interface, which allows the client side to generate a implementation, compile time, that can be parsed from Json code.

Server side:

MyInitData data = new MyInitData("interestingValue");

MyInitDataFactory factory = AutoBeanFactorySource.create(MyInitDataFactory.class);
       
        AutoBean<IMyInitData> autoBean = factory.create(IMyInitData.class, data);
       
        Splittable splittable = AutoBeanCodex.encode(autoBean);
       
        // not strictly needed, but highly advisable if data is dynamic!
        String json = ESAPI.encoder().encodeForJavaScript(splittable.getPayload());
       
        String formattedJson = String.format("var hostPageVar = {\n\tjson : \"%s\"\n}\n", json);

// render the formattedJson inside a script tag on the host page, using spring or some template system

Client side:

MyInitDataFactory factory = GWT.create(MyInitDataFactory.class)

        Dictionary dictionary = Dictionary.getDictionary("hostPageVar");
        String json = dictionary.get("json");

        AutoBean<IMyInitData> autoBean = AutoBeanCodex.decode(factory, IMyInitData.class, json);

        IMyInitData result = autoBean.as();

I hope that this sheds some light on how to pass data to the client through the host page without to much custom code. Google does not really seem to give any hints about how to do this, and I couldn't find any examples really showing how to use the AutoBean system for this.

2 comments

Anonymous

Something that I did not realize before was that though collections and custom interfaces are supported as properties of an AutoBean, collections containing custom interfaces are not.
The reason is that the AutoBean mechanism is supposed to work server and client side. On the client side, there is no support for reflection and generics, so the information about the contents of a list is lost.
This is quite a restriction and basically means that we will need to change the layout of our data to not produce any lists having custom class contents.

Anonymous

In a future post, I hope to run through some of the reasoning behind our choices. Great info here. I really enjoy reading your posts. I truly have learned a vast amount from them.