MS OneDrive JavaScript SDK 处理程序不是全局的?
MS OneDrive JavaScript SDK Handlers Not Global?
我正在尝试实施找到的 v7 OneDrive SDK here,但是当我使用在该页面的示例数组中找到的事件处理程序函数的语法时,我收到此错误:
Uncaught Error: [OneDriveSDK Error] function was missing and not optional
这是我的代码:
<script type="text/javascript">
function od_success( files ) {
console.log( 'OneDrive Launch Success!' );
}
function od_error(e) {
console.log( 'OneDrive Launch Failed!' );
}
function od_cancel() {
console.log( 'OneDrive Launch Cancelled!' );
}
function launchOneDrivePicker() {
var odOptions = {
clientId: <?php echo '"' . esc_attr( get_option( 'rod_api_key' ) ) . '"'; ?>,
action: "query",
openInNewWindow: false,
oneDriveFilePickerSuccess: function(files) { od_success(files) },
oneDriveFilePickerCancel: function() { od_cancel() },
oneDriveFilePickerError: function(e) { od_error(e) }
};
OneDrive.open( odOptions );
}
</script>
(注意:这一切都发生在 WordPress 页面的上下文中,并在头部吐出,而 SDK 在页脚中排队。)
我试过只调用命名函数,如下所示:
oneDriveFilePickerSuccess: od_success(files),
oneDriveFilePickerCancel: od_cancel,
oneDriveFilePickerError: od_error(e)
这只在不传递参数的情况下有效(例如,od_success()
),但在那种情况下,它会依次调用所有三个参数,然后给出上述错误。如果我如上所述将参数添加到调用中,它会告诉我 files
未定义。
在此先感谢您的帮助
您的函数是匿名函数,为了使它们成为这种意义上的全局函数,您必须:
function oneDriveFilePickerSuccess( files ) {
console.log( 'OneDrive Launch Success!' );
}
function oneDriveFilePickerError() {
console.log( 'OneDrive Launch Failed!' );
}
function oneDriveFilePickerCancel( e ) {
console.log( 'OneDrive Launch Cancelled!' );
}
function launchOneDrivePicker() {
var odOptions = {
clientId: <?php echo '"' . esc_attr( get_option( 'rod_api_key' ) ) . '"'; ?>,
action: "query",
openInNewWindow: false,
success: 'oneDriveFilePickerSuccess',
cancel: 'oneDriveFilePickerCancel',
error: 'oneDriveFilePickerError'
};
OneDrive.open( odOptions );
}
通过名称引用函数很重要,而不是将它们包装成匿名函数或移交对它们的引用。
希望对您有所帮助。
您是否在此代码段之前或之后添加了 OneDrive.js
脚本?我认为他们所说的 "declared globally before referencing the SDK" 的意思是你首先声明你的回调函数,然后包含他们的 SDK 脚本,这样 SDK 就可以注册这些函数(这就是为什么正确命名这些函数很重要)。这意味着您必须像
这样拆分代码
<script>
function oneDriveFilePickerSuccess(files) {
console.log('OneDrive Launch Success!');
}
function oneDriveFilePickerError() {
console.log('OneDrive Launch Failed!');
}
function oneDriveFilePickerCancel(e) {
console.log('OneDrive Launch Cancelled!');
}
</script>
<script src="https://js.live.net/v7.0/OneDrive.js"></script>
<script>
function launchOneDrivePicker() {
// ...
}
</script>
在一个脚本中,函数声明的位置无关紧要,但每个脚本标记都是独立执行的,所以它很重要。
参见文档:
Note: If openInNewWindow is false, then all callback functions must be declared globally on the page before the SDK is referenced to guarantee the functions will be called. When registered globally the callback function names are renamed with a prefix of oneDriveFilePicker. For example, success becomes oneDriveFilePickerSuccess.
您必须像这样定义选项:
var oneDriveFilePickerError =
function () {
console.log( 'OneDrive Launch Failed!' );
}
var oneDriveFilePickerSuccess =
function ( files ) {
console.log( 'OneDrive Launch Success!' );
}
var oneDriveFilePickerCancel =
function ( e ) {
console.log( 'OneDrive Launch Cancelled!' );
}
function launchOneDrivePicker() {
var odOptions = {
clientId: '...',
action: "query",
openInNewWindow: false,
success: 'oneDriveFilePickerSuccess',
cancel: 'oneDriveFilePickerCancel',
error: 'oneDriveFilePickerError'
};
OneDrive.open( odOptions );
}
launchOneDrivePicker()
我正在尝试实施找到的 v7 OneDrive SDK here,但是当我使用在该页面的示例数组中找到的事件处理程序函数的语法时,我收到此错误:
Uncaught Error: [OneDriveSDK Error] function was missing and not optional
这是我的代码:
<script type="text/javascript">
function od_success( files ) {
console.log( 'OneDrive Launch Success!' );
}
function od_error(e) {
console.log( 'OneDrive Launch Failed!' );
}
function od_cancel() {
console.log( 'OneDrive Launch Cancelled!' );
}
function launchOneDrivePicker() {
var odOptions = {
clientId: <?php echo '"' . esc_attr( get_option( 'rod_api_key' ) ) . '"'; ?>,
action: "query",
openInNewWindow: false,
oneDriveFilePickerSuccess: function(files) { od_success(files) },
oneDriveFilePickerCancel: function() { od_cancel() },
oneDriveFilePickerError: function(e) { od_error(e) }
};
OneDrive.open( odOptions );
}
</script>
(注意:这一切都发生在 WordPress 页面的上下文中,并在头部吐出,而 SDK 在页脚中排队。)
我试过只调用命名函数,如下所示:
oneDriveFilePickerSuccess: od_success(files),
oneDriveFilePickerCancel: od_cancel,
oneDriveFilePickerError: od_error(e)
这只在不传递参数的情况下有效(例如,od_success()
),但在那种情况下,它会依次调用所有三个参数,然后给出上述错误。如果我如上所述将参数添加到调用中,它会告诉我 files
未定义。
在此先感谢您的帮助
您的函数是匿名函数,为了使它们成为这种意义上的全局函数,您必须:
function oneDriveFilePickerSuccess( files ) {
console.log( 'OneDrive Launch Success!' );
}
function oneDriveFilePickerError() {
console.log( 'OneDrive Launch Failed!' );
}
function oneDriveFilePickerCancel( e ) {
console.log( 'OneDrive Launch Cancelled!' );
}
function launchOneDrivePicker() {
var odOptions = {
clientId: <?php echo '"' . esc_attr( get_option( 'rod_api_key' ) ) . '"'; ?>,
action: "query",
openInNewWindow: false,
success: 'oneDriveFilePickerSuccess',
cancel: 'oneDriveFilePickerCancel',
error: 'oneDriveFilePickerError'
};
OneDrive.open( odOptions );
}
通过名称引用函数很重要,而不是将它们包装成匿名函数或移交对它们的引用。
希望对您有所帮助。
您是否在此代码段之前或之后添加了 OneDrive.js
脚本?我认为他们所说的 "declared globally before referencing the SDK" 的意思是你首先声明你的回调函数,然后包含他们的 SDK 脚本,这样 SDK 就可以注册这些函数(这就是为什么正确命名这些函数很重要)。这意味着您必须像
<script>
function oneDriveFilePickerSuccess(files) {
console.log('OneDrive Launch Success!');
}
function oneDriveFilePickerError() {
console.log('OneDrive Launch Failed!');
}
function oneDriveFilePickerCancel(e) {
console.log('OneDrive Launch Cancelled!');
}
</script>
<script src="https://js.live.net/v7.0/OneDrive.js"></script>
<script>
function launchOneDrivePicker() {
// ...
}
</script>
在一个脚本中,函数声明的位置无关紧要,但每个脚本标记都是独立执行的,所以它很重要。
参见文档:
Note: If openInNewWindow is false, then all callback functions must be declared globally on the page before the SDK is referenced to guarantee the functions will be called. When registered globally the callback function names are renamed with a prefix of oneDriveFilePicker. For example, success becomes oneDriveFilePickerSuccess.
您必须像这样定义选项:
var oneDriveFilePickerError =
function () {
console.log( 'OneDrive Launch Failed!' );
}
var oneDriveFilePickerSuccess =
function ( files ) {
console.log( 'OneDrive Launch Success!' );
}
var oneDriveFilePickerCancel =
function ( e ) {
console.log( 'OneDrive Launch Cancelled!' );
}
function launchOneDrivePicker() {
var odOptions = {
clientId: '...',
action: "query",
openInNewWindow: false,
success: 'oneDriveFilePickerSuccess',
cancel: 'oneDriveFilePickerCancel',
error: 'oneDriveFilePickerError'
};
OneDrive.open( odOptions );
}
launchOneDrivePicker()