Created
May 25, 2015 19:08
-
-
Save alex-wilmer/502c878ab1a21ec4ae2d to your computer and use it in GitHub Desktop.
This file contains 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
function $list ($http) { | |
var headers = {'Accept': 'application/json;odata=verbose'} | |
, getList = function (listName, options) { | |
var site = options && options.site && '/' + options.site || ''; | |
return $http({ | |
method: 'GET' | |
, url: site + '/_api/web/lists/getByTitle(\'' + listName + '\')' | |
, headers: headers | |
}) | |
.then(function (response) { | |
return response.data.d; | |
}); | |
} | |
, getItemsByUrl = function (url, items) { | |
return $http({ | |
method: 'GET' | |
, url: url | |
, headers: headers | |
}) | |
.then(function (response) { | |
items = items.concat(response.data.d.results); | |
if (response.data.d.__next) { | |
return getItemsByUrl(response.data.d.__next, items); | |
} | |
else return items; | |
}); | |
} | |
, getItemsByListName = function (listName, options, items) { | |
var site = options && options.site && '/' + options.site || ''; | |
if (options) { | |
var tmp = [] | |
, optionsString = (function () { | |
angular.forEach(options, function(value, key) { | |
if (key !== 'site') { | |
tmp.push('$' + key + '=' + value); | |
} | |
}); | |
return tmp.join('&'); | |
}()) | |
} | |
else options = ''; | |
return $http({ | |
method: 'GET' | |
, url: site + '/_api/web/lists/getByTitle(\'' + listName + '\')/items?' | |
+ optionsString | |
, headers: headers | |
}) | |
.then(function (response) { | |
items = items.concat(response.data.d.results); | |
if (response.data.d.__next && !options.top) { | |
return getItemsByUrl(response.data.d.__next, items); | |
} | |
else return items; | |
}); | |
} | |
, getItemByListName = function (listName, id, site) { | |
var site = site && '/' + site || ''; | |
return $http({ | |
method: 'GET' | |
, url: site + '/_api/web/lists/getByTitle(\'' + listName + '\')/items(' + id + ')' | |
, headers: headers | |
}) | |
.then(function (response) { | |
return response.data.d; | |
}); | |
} | |
, getFieldByName = function (listName, fieldName, site) { | |
var site = site && '/' + site || ''; | |
return $http({ | |
method: 'GET' | |
, url: site + '/_api/web/lists/getByTitle(\'' + listName + '\')/fields/getByTitle(\'' + fieldName + '\')' | |
, headers: headers | |
}) | |
.then(function (response) { | |
return response.data.d; | |
}); | |
} | |
, insertItem = function (listName, data, callback) { | |
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () { | |
var context = SP.ClientContext.get_current() | |
var list = context.get_web().get_lists().getByTitle(listName) | |
var item = list.addItem(new SP.ListItemCreationInformation()) | |
for (prop in data) { | |
item.set_item(prop, data[prop]) | |
} | |
item.update() | |
context.executeQueryAsync( | |
function () { | |
callback('Success!') | |
} | |
, function () { | |
callback('Fail!') | |
}) | |
}) | |
} | |
return { | |
get: function (listName, options) { | |
return getList(listName, options); | |
} | |
, getItems: function (source, options) { | |
if (source.indexOf('/') === -1) | |
return getItemsByListName(source, options, []) | |
else | |
return getItemsByUrl(source,[]) | |
} | |
, getItem: function (listName, id, site) { | |
return getItemByListName(listName, id, site) | |
} | |
, getField: function (listName, fieldName, site) { | |
return getFieldByName(listName, fieldName, site) | |
} | |
, insert: function (listName, data) { | |
insertItem(listname, data) | |
} | |
} | |
} | |
angular | |
.module('CSNapp') | |
.factory('$list', $list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment