音位间隙和提示()
我正在查看 Android 的 Phonegap 源代码,并尝试验证他们的 notification.alert()
方法只需委托给原生 JavaScript alert()
函数即可。他们的代码是这样的:
Notification.prototype.alert = function(message, completeCallback, title, buttonLabel) {
var _title = (title || "Alert");
var _buttonLabel = (buttonLabel || "OK");
PhoneGap.exec(completeCallback, null, "Notification", "alert", [message,_title,_buttonLabel]);
};
在我看来,“alert”将被解释为要在 exec() 中调用的函数的名称,但是 exec() 正在做:
PhoneGap.exec = function(success, fail, service, action, args) {
try {
var callbackId = service + PhoneGap.callbackId++;
if (success || fail) {
PhoneGap.callbacks[callbackId] = {success:success, fail:fail};
}
//using: ["Notification", "alert", callbackId, true]
var r = prompt(PhoneGap.stringify(args),
"gap:"+PhoneGap.stringify([service, action, callbackId, true]));
//...
} catch (e2) {
console.log("Error: "+e2);
}
};
现在PhoneGap.stringify()
只是解析为 JSON.stringify()
,因此 Phonegap 代码通过以下方式执行 notification.alert()
API 方法:呼叫具有两个 JSON 对象/数组的 prompt()
函数。我的假设是 prompt()
是 本机 JavaScript Prompt() 函数(我在他们的 JavaScript 代码中没有发现任何可以覆盖此函数的内容)。如果是这样的话,那么这段代码是如何工作的呢?
他们在其他各个地方也使用了类似的 prompt()
:
PhoneGap.JSCallbackPort = prompt("getPort", "gap_callbackServer:");
他们调用 prompt()
的方式有什么特别之处吗(特别是通过包含第二个参数的形式 gap.*:.*
) 启用某些自定义行为?或者他们是否以某种方式覆盖了 JavaScript 代码外部的 prompt()
函数的默认行为?
请注意,这特别适用于 Android 版本的 Phonegap,因为其他版本似乎使用略有不同的机制来执行 API 调用。
I was looking through the Phonegap sources for Android, and trying to verify that their notification.alert()
method simply delegates to the native JavaScript alert()
function. Their code does:
Notification.prototype.alert = function(message, completeCallback, title, buttonLabel) {
var _title = (title || "Alert");
var _buttonLabel = (buttonLabel || "OK");
PhoneGap.exec(completeCallback, null, "Notification", "alert", [message,_title,_buttonLabel]);
};
Which looks to me like "alert" will be interpreted as the name of the function to invoke in exec()
, but exec()
is doing:
PhoneGap.exec = function(success, fail, service, action, args) {
try {
var callbackId = service + PhoneGap.callbackId++;
if (success || fail) {
PhoneGap.callbacks[callbackId] = {success:success, fail:fail};
}
//using: ["Notification", "alert", callbackId, true]
var r = prompt(PhoneGap.stringify(args),
"gap:"+PhoneGap.stringify([service, action, callbackId, true]));
//...
} catch (e2) {
console.log("Error: "+e2);
}
};
Now PhoneGap.stringify()
simply resolves to JSON.stringify()
, so the Phonegap code is executing the notification.alert()
API method by calling the prompt()
function with two JSON objects/arrays. My assumption is that prompt()
is the native JavaScript prompt() function (I've not found anything in their JavaScript code that would override this function). If that is the case, then how is this code working?
They make similar use of prompt()
in various other places, as well:
PhoneGap.JSCallbackPort = prompt("getPort", "gap_callbackServer:");
Is there something special about the way they are calling prompt()
(specifically by including a second parameter of the form gap.*:.*
) that is enabling some custom behavior? Or have they somehow overridden the default behavior of the prompt()
function somewhere external to their JavaScript code?
Note that this applies specifically to the Android version of Phonegap, as other versions seem to use slightly different mechanisms for executing the API calls.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
prompt()
函数已被覆盖。您可以在 DroidGap.java 中找到它。
The
prompt()
function has been overridden.You can find that in DroidGap.java.
JavaScript 到 Java 的桥接器在 Android 2.3 中的模拟器上停止工作。一位聪明的 PhoneGap 贡献者发现,按提示进行支持是一种解决方法。
这是 Android 问题。
The JavaScript to Java bridge stopped working on the emulator in Android 2.3. A clever PhoneGap contributor discovered that piggy backing on prompt was a workaround.
Here is the Android issue.