0

I am trying to set the state in a Vaadin custom widget from a click generated by the user. In the component connector after instantiating the server rpc I get the GWT button and add an onclick method. In the method I set the state (getState.text = "new text";) but when I try to get it from the server side I get the original state text. The onStateChange method is not triggered.

Code in the connector:

        getWidget().getSaveButton().addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            alert("does it work without jquery?");
            getState().text = "text changed from connector";
            getWidget().getTextBox().setText(getState().text);
        }
    });

after clicking GWT button the textbox contains the text to "text changed from connector"

In the state:

public String text = "original state text";

The UI implementation to get the state:

vaadinButton.addClickListener(new ClickListener() {

        @Override
        public void buttonClick(ClickEvent event) {
            Notification.show(sig.getState().text);

        }
    });

When clicking the vaadin button after clicking the GWT button the notification still displays "original state text".

My question: How do I change the state in the connector so it triggers the onStateChange method and I get the text changes server side?

FCoffee
  • 116
  • 1
  • 5

1 Answers1

1

It's not possible to update state from the client side. Only server can update state. You should do so that you sent a RPC request from client to server, and on the server you update the value to state.

Edit, here is an example on how to send a value from client to server using Vaadin's RPC mechanism:

Create an interface extending ServerRpc:

public interface MyServerRpc extends com.vaadin.shared.communication.ServerRpc {
  void setValue(String value)
}

Use a proxy created from the above interface to send value to server. This call is done inside the connector.

getRpcProxy(MyServerRpc.class).setValue("New value to server");

Register an interface implementation to your server-side component to receive RPC calls:

registerRpc(new MyServerRpc() {

  public void setValue(String value) {
    // set the value to state here by saying getState().myValue = value;
    // or do something else
  }
});
Henri Kerola
  • 4,947
  • 1
  • 17
  • 19
  • Thanks very much Henri. It has been a little while since I've seen you. I'm the coffee spitter from the New York class – FCoffee Mar 02 '15 at 17:42
  • @FCoffee, hah, I hope that everything is going great there. – Henri Kerola Mar 02 '15 at 17:57
  • Things are going well. I haven't coughed any drinks on anyone since:) The problem I want to solve is I am trying to get a json string back from the client side widget. According to your answer I can't do this through the state. Is the only way to do this pre-existing Vaadin Components? As in take the json string and place it in a Vaadin TextArea and then get the text Server Side. I would probably place the custom widget inside a Composite Component with a TextArea. – FCoffee Mar 02 '15 at 20:11
  • You can use RPC for that. I editor the answer and added an example. – Henri Kerola Mar 03 '15 at 06:06
  • Your example helped me immensely. I am able to change the state. Thank you very much Henri. – FCoffee Mar 03 '15 at 18:42