summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLeif Åstrand <leif@vaadin.com>2015-01-14 10:15:03 +0200
committerLeif Åstrand <leif@vaadin.com>2015-01-14 10:21:53 +0200
commit61430e669b3bbbd702373791854650bc3844c11f (patch)
treec15da61282def61730cbe703a02b5d24f6dcf507
parent032bcef30f5ada7dc1ca6eade07ee596afa25bb3 (diff)
downloadvaadin-framework-61430e669b3bbbd702373791854650bc3844c11f.tar.gz
vaadin-framework-61430e669b3bbbd702373791854650bc3844c11f.zip
Update JS renderer to work with latest grid branch (#15485)
* Cope with createRenderer not being run deferred * Update test to not assume there's a selection column Change-Id: Ic6f053d2ef76d7227eb9ca00b960629e34ae380c
-rw-r--r--client/src/com/vaadin/client/JavaScriptConnectorHelper.java97
-rw-r--r--client/src/com/vaadin/client/connectors/JavaScriptRendererConnector.java2
-rw-r--r--uitest/src/com/vaadin/tests/components/grid/JavaScriptRenderersTest.java10
3 files changed, 70 insertions, 39 deletions
diff --git a/client/src/com/vaadin/client/JavaScriptConnectorHelper.java b/client/src/com/vaadin/client/JavaScriptConnectorHelper.java
index 3a9a6198d3..1eb326115e 100644
--- a/client/src/com/vaadin/client/JavaScriptConnectorHelper.java
+++ b/client/src/com/vaadin/client/JavaScriptConnectorHelper.java
@@ -59,50 +59,79 @@ public class JavaScriptConnectorHelper {
rpcObjects.put("", JavaScriptObject.createObject());
}
+ /**
+ * The id of the previous response for which state changes have been
+ * processed. If this is the same as the
+ * {@link ApplicationConnection#getLastResponseId()}, it means that the
+ * state change has already been handled and should not be done again.
+ */
+ private int processedResponseId = -1;
+
public void init() {
connector.addStateChangeHandler(new StateChangeHandler() {
@Override
public void onStateChanged(StateChangeEvent stateChangeEvent) {
- JavaScriptObject wrapper = getConnectorWrapper();
- JavaScriptConnectorState state = getConnectorState();
+ processStateChanges();
+ }
+ });
+ }
- for (String callback : state.getCallbackNames()) {
- ensureCallback(JavaScriptConnectorHelper.this, wrapper,
- callback);
- }
+ /**
+ * Makes sure the javascript part of the connector has been initialized. The
+ * javascript is usually initalized the first time a state change event is
+ * received, but it might in some cases be necessary to make this happen
+ * earlier.
+ *
+ * @since 7.4.0
+ */
+ public void ensureJavascriptInited() {
+ if (initFunctionName == null) {
+ processStateChanges();
+ }
+ }
+
+ private void processStateChanges() {
+ int lastResponseId = connector.getConnection().getLastResponseId();
+ if (processedResponseId == lastResponseId) {
+ return;
+ }
+ processedResponseId = lastResponseId;
- for (Entry<String, Set<String>> entry : state
- .getRpcInterfaces().entrySet()) {
- String rpcName = entry.getKey();
- String jsName = getJsInterfaceName(rpcName);
- if (!rpcObjects.containsKey(jsName)) {
- Set<String> methods = entry.getValue();
- rpcObjects.put(jsName,
- createRpcObject(rpcName, methods));
-
- // Init all methods for wildcard rpc
- for (String method : methods) {
- JavaScriptObject wildcardRpcObject = rpcObjects
- .get("");
- Set<String> interfaces = rpcMethods.get(method);
- if (interfaces == null) {
- interfaces = new HashSet<String>();
- rpcMethods.put(method, interfaces);
- attachRpcMethod(wildcardRpcObject, null, method);
- }
- interfaces.add(rpcName);
- }
+ JavaScriptObject wrapper = getConnectorWrapper();
+ JavaScriptConnectorState state = getConnectorState();
+
+ for (String callback : state.getCallbackNames()) {
+ ensureCallback(JavaScriptConnectorHelper.this, wrapper, callback);
+ }
+
+ for (Entry<String, Set<String>> entry : state.getRpcInterfaces()
+ .entrySet()) {
+ String rpcName = entry.getKey();
+ String jsName = getJsInterfaceName(rpcName);
+ if (!rpcObjects.containsKey(jsName)) {
+ Set<String> methods = entry.getValue();
+ rpcObjects.put(jsName, createRpcObject(rpcName, methods));
+
+ // Init all methods for wildcard rpc
+ for (String method : methods) {
+ JavaScriptObject wildcardRpcObject = rpcObjects.get("");
+ Set<String> interfaces = rpcMethods.get(method);
+ if (interfaces == null) {
+ interfaces = new HashSet<String>();
+ rpcMethods.put(method, interfaces);
+ attachRpcMethod(wildcardRpcObject, null, method);
}
+ interfaces.add(rpcName);
}
+ }
+ }
- // Init after setting up callbacks & rpc
- if (initFunctionName == null) {
- initJavaScript();
- }
+ // Init after setting up callbacks & rpc
+ if (initFunctionName == null) {
+ initJavaScript();
+ }
- invokeIfPresent(wrapper, "onStateChange");
- }
- });
+ invokeIfPresent(wrapper, "onStateChange");
}
private static String getJsInterfaceName(String rpcName) {
diff --git a/client/src/com/vaadin/client/connectors/JavaScriptRendererConnector.java b/client/src/com/vaadin/client/connectors/JavaScriptRendererConnector.java
index a7036342f0..2670a3e184 100644
--- a/client/src/com/vaadin/client/connectors/JavaScriptRendererConnector.java
+++ b/client/src/com/vaadin/client/connectors/JavaScriptRendererConnector.java
@@ -132,6 +132,8 @@ public class JavaScriptRendererConnector extends
@Override
protected Renderer<JsonValue> createRenderer() {
+ helper.ensureJavascriptInited();
+
if (!hasFunction("render")) {
throw new RuntimeException("JavaScriptRenderer "
+ helper.getInitFunctionName()
diff --git a/uitest/src/com/vaadin/tests/components/grid/JavaScriptRenderersTest.java b/uitest/src/com/vaadin/tests/components/grid/JavaScriptRenderersTest.java
index a3bb736086..96fd672ab1 100644
--- a/uitest/src/com/vaadin/tests/components/grid/JavaScriptRenderersTest.java
+++ b/uitest/src/com/vaadin/tests/components/grid/JavaScriptRenderersTest.java
@@ -30,17 +30,17 @@ public class JavaScriptRenderersTest extends MultiBrowserTest {
openTestURL();
GridElement grid = $(GridElement.class).first();
- GridCellElement cell_1_2 = grid.getCell(1, 2);
+ GridCellElement cell_1_1 = grid.getCell(1, 1);
// Verify render functionality
- Assert.assertEquals("Bean(2, 0)", cell_1_2.getText());
+ Assert.assertEquals("Bean(2, 0)", cell_1_1.getText());
// Verify init functionality
- Assert.assertEquals("2", cell_1_2.getAttribute("column"));
+ Assert.assertEquals("1", cell_1_1.getAttribute("column"));
// Verify onbrowserevent
- cell_1_2.click();
- Assert.assertTrue(cell_1_2.getText().startsWith(
+ cell_1_1.click();
+ Assert.assertTrue(cell_1_1.getText().startsWith(
"Clicked 1 with key 1 at"));
}
}