我在我的 JSOM ( https://msdn.microsoft.com/en-us/library/ff411788(v=office.14).aspx )中使用了相当多的 SP.SOD.executeOrDelayUntilScriptLoaded(func, depScriptFileName )。有没有人成功使用 SP.SOD.executeOrDelayUntilEventNotified(func, eventName) ( https://msdn.microsoft.com/en-us/library/ff410354(v=office.14).aspx )?对于 eventName 是否像“点击”一样简单?我在网上搜索过,但没有发现任何有用的东西。任何反馈表示赞赏。
1 回答
基本上,这些函数之间的区别在于,在第一种情况下,指定了客户端库中的文件名,例如sp.js
(参数depScriptFileName
)。在后一种情况下,应指定事件名称,例如"sp.scriptloaded-sp.js"
(参数eventName
)
这是SP.SOD.executeOrDelayUntilEventNotified(func, eventName)
来自 SharePoint 客户端库的声明init.js
:
function ExecuteOrDelayUntilScriptLoaded(func, depScriptFileName) {
depScriptFileName = depScriptFileName.toLowerCase();
var eventName = "sp.scriptloaded-" + depScriptFileName;
return ExecuteOrDelayUntilEventNotified(func, eventName);
}
关于事件名称
事件名称列表存储在名为 的全局变量中g_ExecuteOrWaitJobs
。对于每个 SharePoint 客户端库文件,都使用一个预定义的事件名称,例如,对于一个文件sp.clienttemplates.js
,对应的事件名称是sp.scriptloaded-clienttemplates.js
让我们演示如何同时使用SP.SOD.executeOrDelayUntilScriptLoaded(func, depScriptFileName)
和SP.SOD.executeOrDelayUntilEventNotified(func, eventName)
函数。
为此,让我们介绍一个打印SP.Web
Title 属性的简单示例:
function printWebInfo(){
var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
ctx.load(web,'Title');
ctx.executeQueryAsync(
function(){
console.log(web.get_title());
},
function(sender,args){
console.log(args.get_message());
});
}
在下面的例子中
ExecuteOrDelayUntilScriptLoaded(printWebInfo, "sp.js");
printWebInfo
sp.js
加载SharePoint 客户端库后将调用函数。
使用的相同示例SP.SOD.executeOrDelayUntilEventNotified(func, eventName)
如下所示:
var eventName = "sp.scriptloaded-sp.js";
ExecuteOrDelayUntilEventNotified(printWebInfo,eventName);
其中"sp.scriptloaded-sp.js"
事件名称用于确定是否sp.js
加载了库。