Created
November 27, 2014 11:53
-
-
Save phillipharding/05747f21b0cf870134e9 to your computer and use it in GitHub Desktop.
Get AppWeb Lists using a Promises Pattern (or chaining) Allowing for Enrichment/Further Processing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* jQuery DOM ready */ | |
$(function() { | |
JSRequest.EnsureSetup(); | |
GetAppWebLists() | |
.then(EnrichData) | |
.then(RenderAppWebLists) | |
.fail(function(error) { | |
$(".content-body").html(error).show(); | |
}); | |
}); | |
function GetAppWebLists() { | |
var | |
p = new $.Deferred(), | |
appWeburl = decodeURIComponent(JSRequest.QueryString["SPAppWebUrl"]), | |
r = { | |
url: appWeburl + '/_api/web/lists', | |
type: 'GET', | |
headers: { ACCEPT: 'application/json;odata=verbose' } | |
}; | |
$.ajax(r) | |
.done(function(response, textStatus, xhrObj) { | |
var data = (response.value || response.d.results || response.d); | |
/* you might want to mangle or otherwise process the REST results | |
here before resolving the promise with the data | |
*/ | |
p.resolve(data); | |
}) | |
.fail(function(xhrObj, textStatus, err) { | |
var | |
e = JSON.parse(xhrObj.responseText || "{}"), | |
err = e.error || e["odata.error"], | |
m = '<div style="color:red;font-family:Calibri;font-size:1.2em;">Exception<br/>» ' + | |
((err && err.message && err.message.value) ? err.message.value : (xhrObj.status + ' ' + xhrObj.statusText)) | |
+' <br/>» '+r.url+'</div>'; | |
p.reject(m); | |
}); | |
return p.promise(); | |
} | |
function EnrichData(data) { | |
$.each(data, function(i,e) { | |
e.Title = e.Title + " ("+e.Title.toUpperCase()+")"; | |
}); | |
/* either return the data we were passed or return a new Promise and resolve/reject it here */ | |
return data; | |
} | |
function RenderAppWebLists(data) { | |
$(".content-body").empty().show(); | |
$ul = $("<UL/>"); | |
$.each(data, function(i,e) { | |
$ul.append("<li>"+e.Title+"</li>"); | |
}); | |
$(".content-body").append("<h2>AppWeb Lists</h2>").append($ul); | |
/* either return the data we were passed or return a new Promise and resolve/reject it here */ | |
return data; | |
} |
Thanks. Sorry I missed both you at SharePoint Stat.. I am the chap who 'won' the pebble.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sure, the key is the way promises are used to chain functions together, regardless of what those functions do