Skip to content

Instantly share code, notes, and snippets.

@eirikb

eirikb/app.html Secret

Created May 18, 2015 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eirikb/0a35dd446801e5a3bfe2 to your computer and use it in GitHub Desktop.
Save eirikb/0a35dd446801e5a3bfe2 to your computer and use it in GitHub Desktop.
execThen
<div ng-controller="UserCtrl">
<p>Hello {{user.get_title()}}</p>
</div>
<div ng-controller="ListsCtrl">
<p>All lists:</p>
<ul>
<li ng-repeat="list in lists">
{{list.get_title()}}
</li>
</ul>
</div>
<div ng-controller="SiteUsersCtrl">
<p>All users:</p>
<ul>
<li ng-repeat="user in users">
{{user.get_title()}}
</li>
</ul>
</div>
app.factory('ctx', function ($q) {
var ctx = new SP.ClientContext("/");
ctx.execThen = (function () {
var q;
return function () {
if (!q) {
q = $q.when().then(function () {
return $q(function (resolve, reject) {
ctx.executeQueryAsync(resolve, reject);
}).then(function () {
q = null;
});
});
}
return q;
};
})();
return ctx;
});
app.controller('UserCtrl', function ($scope, ctx) {
var user = ctx.get_web().get_currentUser();
ctx.load(user);
ctx.execThen().then(function () {
$scope.user = user;
});
});
app.controller('ListsCtrl', function ($scope, ctx) {
var lists = ctx.get_web().get_lists();
ctx.load(lists);
ctx.execThen().then(function () {
$scope.lists = [];
var e = lists.getEnumerator();
while (e.moveNext()) $scope.lists.push(e.get_current());
});
});
app.controller('SiteUsersCtrl', function ($scope, ctx) {
var users = ctx.get_web().get_siteUsers();
ctx.load(users);
ctx.execThen().then(function () {
$scope.users = [];
var e = users.getEnumerator();
while (e.moveNext()) $scope.users.push(e.get_current());
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment