【发布时间】:2019-01-10 10:17:35
【问题描述】:
我正在使用 SharePoint 进行 JSOM 调用。我需要得到结果并且在检索到所有数据之前不要继续前进。我已经尝试了很多示例(没有一个足够完整,我无法理解如何使用嵌套的 $.each 循环语句来解决我的问题。我似乎接近了,但没有任何东西可以正常工作。
我已经编辑了我的实际代码(减去从另一个页面传入的前 3 个变量)以合并 Tomalak 的工作,以便我们可以更好地解决它。目前,结果出现了空对象。试图弄清楚我做错了什么。
[2018 年 8 月 6 日编辑] 终于让它工作了。我发现提供的代码只有两个小问题:-)。我会尝试加粗它们。
var fya = [2008,2009]; //Fiscal Year Array which we use to know what lists to look at
var assignedRecords = []; //Global Reusable Variable
var assignedCourses = ['Math','Science','Reading']; //There might not be records who are associated with a particular course in each list. Wee need to pull the student records (assignedRecords) assoiciated with the above in 2008 and 2009.
SP.ClientContext.prototype.executeQueryPromise = function (items) {
var result = $.Deferred();
this.load(items);
this.executeQueryAsync(function (sender, args) { result.resolve(items) },
function (sender, args) { result.reject(args) });
return result.promise();
};
移动'var arr = [];'在 arrayFromCollection 之外 在同一函数的 e 变量中添加 'var'
var arr = [];
function arrayFromCollection(coll) {
var e = coll.getEnumerator();
while (e.moveNext()) arr.push(e.get_current());
return arr;
};
function queryListPromise(title, course) {
var list = hostWeb.get_lists().getByTitle(title);
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where>'
+ '<Eq><FieldRef Name="Course"/><Value Type="Text">' + course + '</Value></Eq>'
+ '</Where></Query></View>');
return context.executeQueryPromise(list.getItems(camlQuery)).then(arrayFromCollection);
};
function GetAssignedApplications() {
assignedRecords = []; //Need to start empty
var myCourses = assignedCourses;
$.each(myCourses, function (i, myCoursesItem) {
var courseName = myCoursesItem.get_lookupValue();
将“$.forEach”更改为“$.each”
$.each(fya, function (n, fyaItem) {
var listTitle = "FY" + String(fyaItem).substring(2); //FY18 & FY19 are the names of the actual lists.
assignedRecords.push(queryListPromise(listTitle, courseName));
});
});
$.when(assignedRecords).then(function (results) {
return Array.prototype.concat.apply([], results);
}).then(function (items) {
items.forEach(function (item) {
var a = item; //item is empty and this actually runs before arrayFromCollection and it returns duplicate records (4) when there is only 2.
});
}).fail(onError);
};
【问题讨论】:
-
为什么每本书都查询相同的章节?
-
我试图笼统地思考..哈哈。估计不是最好的。最好说 arr1 和 arr2。任何一个数组都可以改变。也没有设定计数。所以说书籍[1]可以有章节[20],然后书籍[2]可能有章节[12]。
-
真的,书籍和章节不应该传递给函数。它们已经存在,并且是以前获得的。
-
您能否更改示例以反映这一点?
-
这样做了。所以基本上,它应该搜索 2 个列表(x 和 y)。在这两个列表中,a、b 和/或 c 都是可能的结果。当它们是结果被推送到一个集合。要查看的列表数量会增加,所以它永远不是固定的数字。
标签: sharepoint jquery-deferred