Skip to content

Commit 008e562

Browse files
author
Paul Stockley
committed
Make use of new JsConstructor available in GWT 2.8.1
1 parent acef204 commit 008e562

File tree

2 files changed

+4
-67
lines changed

2 files changed

+4
-67
lines changed

DOCUMENTATION.md

+1-28
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ references will result in slightly less code being generated by the GWT compiler
133133
}
134134
```
135135

136-
<p>To use one of these components, the easiest way is to pass it's class into <code>React.createElement</code> e.g.</p>
136+
<p>To use one of these components, pass it's class into <code>React.createElement</code> e.g.</p>
137137

138138
```java
139139
//The above StatefulExample component could then be used as follows:
@@ -143,33 +143,6 @@ references will result in slightly less code being generated by the GWT compiler
143143
React.createElement(StatefulExample.class, props);
144144
```
145145

146-
<p>There are a couple of restrictions when using this approach. It will not work if you have disabled class meta data
147-
when compiling your code. Also if you specifiy an altenative name or namespace in the JsType annotation it will not work.
148-
In these cases you need to make use of the <code>ComponentUtils.getCtorFn</code> method and pass in the fully qualified
149-
class name e.g.</p>
150-
151-
```java
152-
// Assuming the StatefulExample class was defined in the package com.react.example, you can
153-
// use ComponentUtils.getCtorFn to reference it as follows:
154-
155-
StatefulExample.Props props = new StatefulExample.Props();
156-
props.aProp = "Some value";
157-
158-
React.createElement(ComponentUtils.getCtorFn("com.react.example.StatefulExample"), props);
159-
160-
//Assuming you defined StatefulExample as
161-
162-
@JsType(name="RenamedStatefullExample", namespace=JsPackage.GLOBAL)
163-
class StatefulExample extends Component<StatefulExample.Props, StatefulExample.State> {
164-
.
165-
.
166-
}
167-
168-
//You would refer to this in React.createElement using
169-
React.createElement(ComponentUtils.getCtorFn("RenamedStatefulExample"), props);
170-
```
171-
172-
173146
## 3. Rendering React Elements
174147

175148
<p>The majority of javascript React code you will see uses something called JSX. This is just a preprocessor that allows you
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package gwt.react.client.components;
22

3-
import gwt.interop.utils.client.collections.JsStringMap;
43
import gwt.interop.utils.client.plainobjects.JsPlainObj;
5-
import gwt.interop.utils.shared.collections.StringMap;
64
import gwt.react.client.proptypes.BaseProps;
75

86
/**
@@ -12,8 +10,6 @@ public class ComponentUtils {
1210
private ComponentUtils() {
1311
}
1412

15-
private static StringMap<ComponentConstructorFn> constructorLookup = JsStringMap.create() ;
16-
1713
/**
1814
* Given the Class of a JsType annotated {@link Component} class, return the constructor function to use in Javascript
1915
* @param cls The Class
@@ -22,39 +18,7 @@ private ComponentUtils() {
2218
* @param <T> The type of {@link Component}
2319
* @return The constructor function
2420
*/
25-
public static <P extends BaseProps, S extends JsPlainObj, T extends Component<P, S>> ComponentConstructorFn<P> getCtorFn(Class<T> cls) {
26-
return getCtorFn(cls, cls.getName());
27-
}
28-
29-
/**
30-
* Given the fully qualified name of a JsType annotated {@link Component} class, return the constructor function to use in Javascript
31-
* @param cls The Class
32-
* @param actualClassName The fully qualified name. This is useful if it does not match the className of cls.
33-
* @param <P>
34-
* @return The constructor function
35-
*/
36-
public static <P extends BaseProps, S extends JsPlainObj, T extends Component<P, S>> ComponentConstructorFn<P> getCtorFn(Class<T> cls, String actualClassName) {
37-
ComponentConstructorFn<P> fn = constructorLookup.get(actualClassName);
38-
39-
if (fn == null) {
40-
fn = getJSConstructorFn(actualClassName);
41-
assert(fn != null);
42-
43-
constructorLookup.put(actualClassName, fn);
44-
}
45-
return fn;
46-
}
47-
48-
/**
49-
* TODO find a more efficient way of obtaining the constructor function
50-
*/
51-
private static native <P extends BaseProps> ComponentConstructorFn<P> getJSConstructorFn(String className) /*-{
52-
var namespaces = className.split(".");
53-
var context = $wnd;
54-
55-
for (var i = 0; i < namespaces.length; i++) {
56-
context = context[namespaces[i]];
57-
}
58-
return context;
59-
}-*/;
21+
public static native <P extends BaseProps, S extends JsPlainObj, T extends Component<P, S>> ComponentConstructorFn<P> getCtorFn(Class<T> cls) /*-{
22+
return [email protected]::jsConstructor;
23+
}-*/;
6024
}

0 commit comments

Comments
 (0)