I want some understanding on two programming patterns I've seen and when to employ them. The question is about callbacks and delegate.
In the first code there are two distinct functions, success and fail, something I've always used, but the second code is different.
function loadWebs() {
var clientContext = new SP.ClientContext.get_current();
this.webs = clientContext.get_web().get_webs();
clientContext.load(this.webs);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onWebsLoaded), Function.createDelegate(this, this.onQueryFailed));
}
function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}
function onWebsLoaded(sender, args) {
for (var i = 0; i < this.webs.get_count(); i++) {
alert(this.webs.itemAt(i).get_title());
}
}
and the second code doesn't appear to use callbacks for the success, instead the success is wrapped in the parent function.
myCtx.load(webs, "Include(Id, Lists)");
myCtx.executeQueryAsync(function () {
// push new SP.ListItemCollections into an array and tell the ctx to fetch them
for (var i = 0; i < webs.get_data().length; i++) {
var web = webs.get_data()[i], itms;
if (checkForProjectsList(web)) {
itms = webs.get_data()[i].get_lists()
.getByTitle("Projects").getItems(query);
queryItemCollections.push(itms);
myCtx.load(itms);
}
}
myCtx.executeQueryAsync(function () {
queryItemCollections.forEach(function (item) {
// do work with individual list items here
console.log(item.getItemAtIndex(0).get_item("Title"));
});
}, genericFailHandler);
}, genericFailHandler);
function genericFailHandler(sender, args) {
console.log(args.get_message());
}
Thanks