我正在http://blog.symprogress.com/2010/11/ribbon-insert-any-web-part-using-javascript/使用来源来处理用户网页部件按钮点击事件。
函数'addWebPart()'调用一个函数'SP.Ribbon.WebPartComponent.getWebPartAdder()',它应该返回adder实例,但有时会返回undefined。
如果我添加一个while循环来等待实例值正确返回,我的VM中的浏览器会暂停一段时间。返回实例时,浏览器会再次响应。这只会在某些情况下发生。
我正在使用SharePoint 2013,我所指的代码部分是:
addWebPart = function (wpCategory, wpTitle) {
var webPartAdder = SP.Ribbon.WebPartComponent.getWebPartAdder();
while (webPartAdder == undefined)
webPartAdder = SP.Ribbon.WebPartComponent.getWebPartAdder();
// ... Other stuff ...
}
如何解决此问题?
答案 0 :(得分:3)
对于任何寻找这个问题答案的人来说,你必须打电话给'LoadWPAdderOnDemand()'函数然后等待事件' _spEventWebPartAdderReady'。然后查询' window.WPAdder':
addWebPartDelayed = function (webPartAdder, wpCategory, wpTitle) {
var webPart = findWebPart(webPartAdder, wpCategory, wpTitle);
if (webPart) {
var zone = WPAdder._zones[0];
var wpid = WPAdder._createWebpartPlaceholderInRte();
WPAdder.addItemToPageByItemIdAndZoneId(webPart.id, zone.id, 0, wpid);
}
else
alert('ERROR: Web part not found! Please try again after sometime.');
},
addWebPart = function (wpCategory, wpTitle) {
var webPartAdder = window.WPAdder;
if (webPartAdder == undefined) {
LoadWPAdderOnDemand();
ExecuteOrDelayUntilEventNotified(
function () {
var webPartAdder = window.WPAdder;
addWebPartDelayed(webPartAdder, wpCategory, wpTitle);
},
"_spEventWebPartAdderReady");
}
else
addWebPartDelayed(webPartAdder, wpCategory, wpTitle);
};