Skip to content

Commit

Permalink
Update JS renderer to work with latest grid branch (#15485)
Browse files Browse the repository at this point in the history
* Cope with createRenderer not being run deferred
* Update test to not assume there's a selection column

Change-Id: Ic6f053d2ef76d7227eb9ca00b960629e34ae380c
  • Loading branch information
Legioth committed Jan 14, 2015
1 parent 032bcef commit 61430e6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 39 deletions.
97 changes: 63 additions & 34 deletions client/src/com/vaadin/client/JavaScriptConnectorHelper.java
Expand Up @@ -59,50 +59,79 @@ public JavaScriptConnectorHelper(ServerConnector connector) {
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) {
Expand Down
Expand Up @@ -132,6 +132,8 @@ private static native boolean hasFunction(JavaScriptObject wrapper,

@Override
protected Renderer<JsonValue> createRenderer() {
helper.ensureJavascriptInited();

if (!hasFunction("render")) {
throw new RuntimeException("JavaScriptRenderer "
+ helper.getInitFunctionName()
Expand Down
Expand Up @@ -30,17 +30,17 @@ public void testJavaScriptRenderer() {
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"));
}
}

0 comments on commit 61430e6

Please sign in to comment.