Friday, September 18, 2015

JavaScript - Tips 5 - JSOM with Deferred / promises

'use strict';
//Global Variable with namespaces
var BasicJSOM = window.BasicJSOM || {};
BasicJSOM.Samples = BasicJSOM.Samples || {};
// This code runs when the DOM is ready
//and creates a context object which is needed to use the SharePoint object model
$(document).ready(function () {
debugger;
BasicJSOM.Samples.SimpleDemo = new BasicJSOM.Samples.WebAndListSamples();
BasicJSOM.Samples.SimpleDemo.getUserName();
debugger;
var readPromise = BasicJSOM.Samples.SimpleDemo.getLists();
if (readPromise != undefined && readPromise != null)
{
readPromise
.done(
function () {
alert('Success');
})
.fail(
function () {
alert('Fail');
})
.always(
function () {
alert('always callback');
}
);
}
});
view raw App.js hosted with ❤ by GitHub
BasicJSOM.Samples.WebAndListSamples = function () {
//private varables
var context = SP.ClientContext.get_current();
var user = context.get_web().get_currentUser();
var lists = context.get_web().get_lists();
//private functions
function _getUserName() {
$('#message').text('Web Absolute url : ' + _spPageContextInfo.webAbsoluteUrl);
context.load(user);
context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
}
function onGetUserNameSuccess() {
$('#message').append('<br/> User Title :' + user.get_title());
}
function onGetUserNameFail(sender, args) {
alert('Failed to get user name. Error:' + args.get_message());
}
function _getLists() {
var dfd = $.Deferred();
context.load(lists);
context.executeQueryAsync(
//Async success callback
function () {
$('#message').append('<br/> List Count ' + lists.get_count());
dfd.resolve();
},
//Async fail callback
function (sender, args) {
alert('Failed to get list names. Error:' + args.get_message());
dfd.reject();
});
return dfd.promise();
}
//register public members
var publicMembers = {
getUserName: _getUserName,
getLists: _getLists
}
return publicMembers;
}
view raw AppManager.js hosted with ❤ by GitHub
<%-- The following 4 lines are ASP.NET directives needed when using SharePoint components --%>
<%@ Page Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" MasterPageFile="~masterurl/default.master" Language="C#" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%-- The markup and script in the following Content element will be placed in the <head> of the page --%>
<asp:Content ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
<script type="text/javascript" src="../Scripts/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>
<meta name="WebPartPageExpansion" content="full" />
<!-- Add your CSS styles to the following file -->
<link rel="Stylesheet" type="text/css" href="../Content/App.css" />
<!-- Add your JavaScript to the following file -->
<script type="text/javascript" src="../Scripts/App.js"></script>
<script type="text/javascript" src="../Scripts/AppManager.js"></script>
</asp:Content>
<%-- The markup in the following Content element will be placed in the TitleArea of the page --%>
<asp:Content ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">
JSOM Samples
</asp:Content>
<%-- The markup and script in the following Content element will be placed in the <body> of the page --%>
<asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">
<div>
<p id="message">
<!-- The following content will be replaced with the user name when you run the app - see App.js -->
initializing...
</p>
</div>
</asp:Content>
view raw Default.aspx hosted with ❤ by GitHub
Also please have a look at this sample too by mkropat https://gist.github.com/mkropat/8811708.js

No comments:

Post a Comment