jQuery Deferred and promise - Error: Object doesn't support property or method 'then'
jQuery Deferred and promise - Error: Object doesn't support property or method 'then'
我可能在这里遗漏了一些非常基本的东西,但我似乎找不到错误,这让我很沮丧。我只是想从我的开发 SharePoint 网站中提取列表(然后是项目,但这里一次提取一件事)。
我已经构建了第一个延迟方法,控制台日志显示它已完成,但随后我得到 "Error: Object doesn't support property or method 'then'",好像 jQuery 不知何故失败了。
作为参考,我正在尝试遵循此处描述的方法:http://www.shillier.com/archive/2013/03/04/using-promises-with-the-javascript-client-object-model-in-sharepoint-2013.aspx
代码如下:
<script src="jquery-1.11.2.js"></script>
<script type="text/javascript">
$(function () {
GetSiteLists.bListsGotten().then(
function (oWebLists) {
// Get Lists Succeeded
alert('Lists Retrieved');
}
, function (sender, args) {
// Get Lists Failed
alert('Lists Not Retrieved');
}
);
});
GetSiteLists = function () {
var bListsGotten = function () {
var deferred = $.Deferred();
var oContext = new SP.ClientContext.get_current();
console.log('oContext instantiated');
var oWeb = oContext.get_web();
console.log('oWeb instantiated');
this.oWebLists = oWeb.get_lists();
console.log('oWebLists command set');
oContext.load(this.oWebLists);
console.log('context load command set');
oContext.executeQueryAsync(
Function.createDelegate(this,
function () { deferred.resolve(this.oWebLists); }),
Function.createDelegate(this,
function (sender, args) { deferred.reject(sender, args); }));
console.log('list retrieval query executed async');
console.log('returning promise');
return deferred.promise;
}
return {
bListsGotten: bListsGotten
}
}();
</script>
promise
是一个函数,您没有调用它。
return deferred.promise()
会解决这个问题。
我可能在这里遗漏了一些非常基本的东西,但我似乎找不到错误,这让我很沮丧。我只是想从我的开发 SharePoint 网站中提取列表(然后是项目,但这里一次提取一件事)。
我已经构建了第一个延迟方法,控制台日志显示它已完成,但随后我得到 "Error: Object doesn't support property or method 'then'",好像 jQuery 不知何故失败了。
作为参考,我正在尝试遵循此处描述的方法:http://www.shillier.com/archive/2013/03/04/using-promises-with-the-javascript-client-object-model-in-sharepoint-2013.aspx
代码如下:
<script src="jquery-1.11.2.js"></script>
<script type="text/javascript">
$(function () {
GetSiteLists.bListsGotten().then(
function (oWebLists) {
// Get Lists Succeeded
alert('Lists Retrieved');
}
, function (sender, args) {
// Get Lists Failed
alert('Lists Not Retrieved');
}
);
});
GetSiteLists = function () {
var bListsGotten = function () {
var deferred = $.Deferred();
var oContext = new SP.ClientContext.get_current();
console.log('oContext instantiated');
var oWeb = oContext.get_web();
console.log('oWeb instantiated');
this.oWebLists = oWeb.get_lists();
console.log('oWebLists command set');
oContext.load(this.oWebLists);
console.log('context load command set');
oContext.executeQueryAsync(
Function.createDelegate(this,
function () { deferred.resolve(this.oWebLists); }),
Function.createDelegate(this,
function (sender, args) { deferred.reject(sender, args); }));
console.log('list retrieval query executed async');
console.log('returning promise');
return deferred.promise;
}
return {
bListsGotten: bListsGotten
}
}();
</script>
promise
是一个函数,您没有调用它。
return deferred.promise()
会解决这个问题。