jQuery deferred/promise 带有 SPService 库
jQuery deferred/promise with SPService library
我正在尝试从 SPService SPGetListItemsJson 获取我的承诺项目。问题是,当调用 SPGetListItemsJson 并且完成 requestPromise 并且延迟已解决时,我希望数据传递到我在 populateDropDownControlWithList 中的匿名函数,但它是未定义的。
function populateDropDownControlWithList(option, control, addSelectValue)
{
if (typeof (addSelectValue) === 'undefined')
{
addSelectValue = false;
}
var selectedIndex = control.val() ? control.val() : -1;
if (addSelectValue)
{
control.append('<option value=-1>Select an Option</option>');
}
var request = SPGetListItemsJson(option);
request.done(function (data) // Expect the json object here but it is undefined
{
$.each(data, function ()
{
controlappend('<option value=' + this.Id + '>' + this.Title + '</option>');
});
});
}
function SPGetListItemsJson(option)
{
var deferred = $.Deferred();
var requestsPromise = $().SPServices.SPGetListItemsJson({
listName: option.Name,
CAMLQuery: option.Query,
CAMLViewFields: option.View,
mappingOverrides: option.Mapping,
debug: option.Debug,
async: option.async
});
requestsPromise.done(function ()
{
deferred.resolveWith(this.data); // Verified this.data is populated with the correct result
});
return deferred.promise();
}
如有任何帮助,我们将不胜感激!
您确定 $().SPServices.SPGetListItemsJson()
returns 是一个承诺吗?
SPServices 文档不明确。它说方法 returns 是一个 JavaScript (普通)对象,但随后继续举例说明 var traineePromise = $().SPServices.SPGetListItemsJson({...})
并继续 $.when(traineePromise).done(...)
。
因此,谨慎的做法是将您的 SPGetListItemsJson()
写成如下:
function SPGetListItemsJson(option) {
var obj = $().SPServices.SPGetListItemsJson({
listName: option.Name,
CAMLQuery: option.Query,
CAMLViewFields: option.View,
mappingOverrides: option.Mapping,
debug: option.Debug,
async: option.async
});
return $.when(obj).then(function () {
return this.data;
});
}
通过进行以下更改,我现在可以让它工作了。我张贴它以防有人觉得这有用。
request.done(function ()
{
$.each(this, function ()
{
control.append('<option value=' + this.ID + '>' + this.Title + '</option>');
});
});
谢谢!
这是我的做法:
function getListOne() {
var dfd = $.Deferred();
$().SPServices({
operation: "GetListItems",
listName: "ListOne",
completefunc: function(data, status) {
console.log("first is done");
if (status == "success") {
//do stuff
dfd.resolve("");
} else {
dfd.reject();
}
}
});
return dfd.promise();
}
function getListTwo() {
var dfd = $.Deferred();
$().SPServices({
operation: "GetListItems",
listName: "ListTwo",
completefunc: function(data, status) {
console.log("second is done");
if (status == "success") {
//do stuff
dfd.resolve("");
} else {
dfd.reject();
}
}
});
return dfd.promise();
}
$.when(getListOne(), getListTwo()).then(function() {
console.log("both are done");
});
更容易证明它正在使用 2 个列表和控制台日志。
我正在尝试从 SPService SPGetListItemsJson 获取我的承诺项目。问题是,当调用 SPGetListItemsJson 并且完成 requestPromise 并且延迟已解决时,我希望数据传递到我在 populateDropDownControlWithList 中的匿名函数,但它是未定义的。
function populateDropDownControlWithList(option, control, addSelectValue)
{
if (typeof (addSelectValue) === 'undefined')
{
addSelectValue = false;
}
var selectedIndex = control.val() ? control.val() : -1;
if (addSelectValue)
{
control.append('<option value=-1>Select an Option</option>');
}
var request = SPGetListItemsJson(option);
request.done(function (data) // Expect the json object here but it is undefined
{
$.each(data, function ()
{
controlappend('<option value=' + this.Id + '>' + this.Title + '</option>');
});
});
}
function SPGetListItemsJson(option)
{
var deferred = $.Deferred();
var requestsPromise = $().SPServices.SPGetListItemsJson({
listName: option.Name,
CAMLQuery: option.Query,
CAMLViewFields: option.View,
mappingOverrides: option.Mapping,
debug: option.Debug,
async: option.async
});
requestsPromise.done(function ()
{
deferred.resolveWith(this.data); // Verified this.data is populated with the correct result
});
return deferred.promise();
}
如有任何帮助,我们将不胜感激!
您确定 $().SPServices.SPGetListItemsJson()
returns 是一个承诺吗?
SPServices 文档不明确。它说方法 returns 是一个 JavaScript (普通)对象,但随后继续举例说明 var traineePromise = $().SPServices.SPGetListItemsJson({...})
并继续 $.when(traineePromise).done(...)
。
因此,谨慎的做法是将您的 SPGetListItemsJson()
写成如下:
function SPGetListItemsJson(option) {
var obj = $().SPServices.SPGetListItemsJson({
listName: option.Name,
CAMLQuery: option.Query,
CAMLViewFields: option.View,
mappingOverrides: option.Mapping,
debug: option.Debug,
async: option.async
});
return $.when(obj).then(function () {
return this.data;
});
}
通过进行以下更改,我现在可以让它工作了。我张贴它以防有人觉得这有用。
request.done(function ()
{
$.each(this, function ()
{
control.append('<option value=' + this.ID + '>' + this.Title + '</option>');
});
});
谢谢!
这是我的做法:
function getListOne() {
var dfd = $.Deferred();
$().SPServices({
operation: "GetListItems",
listName: "ListOne",
completefunc: function(data, status) {
console.log("first is done");
if (status == "success") {
//do stuff
dfd.resolve("");
} else {
dfd.reject();
}
}
});
return dfd.promise();
}
function getListTwo() {
var dfd = $.Deferred();
$().SPServices({
operation: "GetListItems",
listName: "ListTwo",
completefunc: function(data, status) {
console.log("second is done");
if (status == "success") {
//do stuff
dfd.resolve("");
} else {
dfd.reject();
}
}
});
return dfd.promise();
}
$.when(getListOne(), getListTwo()).then(function() {
console.log("both are done");
});
更容易证明它正在使用 2 个列表和控制台日志。