我想知道是否有办法访问或公开由 Vaadin 在客户端管理的数据。即,我在 Vaadin 应用程序内部的服务器上使用了一些数据。在 UI 中,我想包含一个使用这些数据并利用 raphael.js 的可视化小部件。我怀疑使用 gwt 图形库可以做到这一点,但这意味着创建一个看起来像 headf*ck 的自定义 Vaadin 小部件。难道没有更简单的方法,就像在客户端使用纯javascript一样简单吗?
问问题
3002 次
2 回答
4
我使用以下服务器端 API 创建了一个小部件:
private Map<String,Object> attributes = new HashMap<String,Object>();
@Override
public void paintContent(PaintTarget target) throws PaintException {
super.paintContent(target);
for (String key : attributes.keySet()) {
Object value = attributes.get(key);
if (value instanceof Boolean)
target.addAttribute(key, ((Boolean) value).booleanValue());
else if (value instanceof Float)
target.addAttribute(key, ((Float) value).floatValue());
else if (value instanceof Double)
target.addAttribute(key, ((Double) value).doubleValue());
else if (value instanceof Integer)
target.addAttribute(key, ((Integer) value).intValue());
else if (value instanceof Long)
target.addAttribute(key, ((Long) value).longValue());
else if (value instanceof String)
target.addAttribute(key, (String) value);
else if (value instanceof Map<?,?>)
target.addAttribute(key, (Map<?,?>) value);
else if (value instanceof Object[])
target.addAttribute(key, (Object[]) value);
}
// We could also set variables in which values can be returned
// but declaring variables here is not required
}
public void resetAttributes() {
attributes.clear();
}
public void setAttribute(String key, Object value) {
attributes.put(key, value);
}
在客户端,我只是将生成的 UIDL 附加到文档中。
public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
this.client = client;
paintableId = uidl.getId();
shareVars(uidl, getElement());
}
public native void shareVars(UIDL uidl, Element element) /*-{
var el = element;
var ownDoc = element.ownerDocument;
ownDoc.uidl = uidl;
ownDoc.doGraphics();
}-*/;
现在在 doGraphics() 我可以使用 uidl[1]["attname"] 访问 uidl 数据
将它与 IcePush 小部件结合起来,我得到了我需要的所有行为,而且一切都很好。
我很好奇为什么这个解决方案以前没有出现,因为它对我来说看起来很自然,如果你能将这种技术与你提到的技术进行比较,我将不胜感激。
于 2011-02-23T13:22:33.337 回答
2
好吧,您可以只使用纯 JavaScript 并将来自服务器的数据作为简单的 JSON 资源公开,您将在客户端使用对该资源的纯 GET/POST 请求来使用该资源。
可以通过覆盖 AbstractApplicationServlet 中的方法或使用包含 JS 的 CustomLayout 来将 JS 添加到 Vaadin 应用程序。当然还有 Window.executeJavaScript 方法以及用于更小的 JS 代码片段。
不是 Vaadin 的做事方式,但完全可行。
于 2011-02-22T12:26:12.843 回答