]> source.dussan.org Git - vaadin-framework.git/commitdiff
Update JS renderer to work with latest grid branch (#15485)
authorLeif Åstrand <leif@vaadin.com>
Wed, 14 Jan 2015 08:15:03 +0000 (10:15 +0200)
committerLeif Åstrand <leif@vaadin.com>
Wed, 14 Jan 2015 08:21:53 +0000 (10:21 +0200)
* Cope with createRenderer not being run deferred
* Update test to not assume there's a selection column

Change-Id: Ic6f053d2ef76d7227eb9ca00b960629e34ae380c

client/src/com/vaadin/client/JavaScriptConnectorHelper.java
client/src/com/vaadin/client/connectors/JavaScriptRendererConnector.java
uitest/src/com/vaadin/tests/components/grid/JavaScriptRenderersTest.java

index 3a9a6198d32c52301d5181f1ac21cdba9aa3f237..1eb326115e8de4db559148f82c9c5a74908eacda 100644 (file)
@@ -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) {
index a7036342f0e0acc4d4a7c065ddba4ef207e64fd5..2670a3e184fe740529b176bac8482651b6ef05fb 100644 (file)
@@ -132,6 +132,8 @@ public class JavaScriptRendererConnector extends
 
     @Override
     protected Renderer<JsonValue> createRenderer() {
+        helper.ensureJavascriptInited();
+
         if (!hasFunction("render")) {
             throw new RuntimeException("JavaScriptRenderer "
                     + helper.getInitFunctionName()
index a3bb7360862c436cb97910067c855630c4913417..96fd672ab1b6e94813117f6331570f3d3ab94a51 100644 (file)
@@ -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"));
     }
 }