我必须在GWT中开发Circuit Sandbox客户端。 https://unpkg.com/circuit-sdk使用ES6,所以我不能使用GWT JSNI。 我正在尝试在https://circuit.github.io/jssdk.html上编写“Teaser示例来登录和获取对话”。 我不得不说,我的JavaScript知识就像初学者一样。
我想,我有两个选择: 我的第一个选择是使用GWT的JsInterop。 (我已经使用过这个,为这个GWT应用程序实现了Websocket。 因此,我使用了示例“http://www.g-widgets.com/2017/03/16/example-of-using-websockets-in-gwt-with-a-spring-boot-server/”,这样可以正常工作。)
对于Circuit Client,我开始编写以下Java类:
@JsType( isNative = true, namespace = JsPackage.GLOBAL )
public class Client {
@JsConstructor
public Client( final String token, final String readings, final String uri ) {
}
@JsMethod
public native void logon();
// The old JSNI code (not usable here):
// public static native void logon() /*-{
// client.logon()
// .then(user => console.log('Successfully authenticated as ' + user.displayName))
// .then(client.getConversations)
// .then(conversations => conversations.forEach(c => console.log(c.convId)))
// .catch(console.error);
// }-*/;
}
此代码无法正常工作,因为我没有定义javascript模块,也没有实现登录方法。
如何访问javascript模块“Circuit”我如何实现logon()方法? 还有什么办法让这个JsInterop类起作用?
第二个选项:我已使用Babel将https://circuitsandbox.net/sdk转换为ES5。我在GWT应用程序中包含了构建的脚本,并尝试实现登录 方法如下:
client();
public static native void client() /*-{
var client = new $wnd.Circuit.Client({
client_id: '78cafde2f6854ad5ad80a67c532687bc',
scope: 'READ_USER_PROFILE,READ_CONVERSATIONS',
domain: 'circuitsandbox.net'
});
client.logon().then(function (user) {
return console.log('Logged on user:', user);
}).catch(console.error);
}-*/;
当我调用该方法时,我会收到几个错误:
[ERROR] Line xx: missing name after . operator
发生此错误导致JSNI无法编译ES6。
如果我注释掉client.logon javascript方法,我得到另一个错误:
"TypeError: Cannot read property 'Client' of undefined"
var client = new $wnd.Circuit.$wnd.Client(...
也行不通。
任何人都能告诉我我必须做些什么才能让它发挥作用,什么是更好的解决方案?
非常感谢,如果有人能帮助我,我真的很高兴。
答案 0 :(得分:0)
这不是因为Circuit使用ES6,你的代码(在JSNI中)必须使用ES6。你可以用匿名函数替换lambdas;因为catch
是一个关键字,你必须将catch方法作为第二个参数传递给then
方法:
public static native void logon() /*-{
client.logon()
.then(function(user) { console.log('Successfully authenticated as ' + user.displayName); })
.then(client.getConversations)
.then(function(conversations) { conversations.forEach(function(c) { console.log(c.convId)); } }, console.error);
}-*/;
与你的第二个片段类似。