(function () {
app.factory('RESTService', function ($http, $q, $location) {
var factory = {};
var currentsiteurl = _spPageContextInfo.webAbsoluteUrl; //_spPageContextInfo.siteAbsoluteUrl;
factory.getItems = function (listName, url) {
var locdefer = $q.defer();
$http({
headers: {
"Accept": "application/json; odata=verbose"
},
method: 'GET',
async: false,
url: currentsiteurl + "/_api/web/lists/GetByTitle('" + listName + "')/items?" + url
}).then(function successCallback(response) {
locdefer.resolve(response.data.d.results);
}, function errorCallback(response) {
console.log("Error Fetching date from " + listName + "\nPlease refresh the page and try again");
});
return locdefer.promise;
}
factory.getAttachements = function (listName, url) {
var locdefer = $q.defer();
$http({
headers: {
"Accept": "application/json; odata=verbose"
},
method: 'GET',
async: false,
url: currentsiteurl + "/_api/web/lists/GetByTitle('" + listName + "')/items" + url
}).then(function successCallback(response) {
locdefer.resolve(response.data.d.results);
}, function errorCallback(response) {
console.log("Error Fetching date from " + listName + "\nPlease refresh the page and try again");
});
return locdefer.promise;
}
factory.postItem = function (item, listname) {
var deferred = $q.defer();
$http({
url: currentsiteurl + "/_api/web/lists/getbytitle('" + listname + "')/items(" + item.ID + ")",
method: "POST",
async: false,
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": document.getElementById("__REQUESTDIGEST").value,
"content-Type": "application/json;odata=verbose",
"IF-MATCH": "*",
"X-Http-Method": "MERGE"
},
data: JSON.stringify(item)
}).success(function (result) {
deferred.resolve(result);
})
.error(function (result, status) {
alert("Error from Server: Unable to update item.Please refresh the page and try again");
console.log(result);
// deferred.reject(status);
});
return deferred.promise;
}
factory.addItem = function (item, listname) {
var defer = $q.defer();
$http({
headers: {
"Accept": "application/json; odata=verbose"
},
method: 'POST',
async: false,
url: currentsiteurl + "/_api/web/lists/getbytitle('" + listname + "')/items",
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": document.getElementById("__REQUESTDIGEST").value,
"content-Type": "application/json;odata=verbose",
},
data: JSON.stringify(item)
}).success(function (data) {
defer.resolve(data);
//alert("Item Created");
}).error(function (msg, x) {
console.log(msg);
console.log(x);
console.log("Error from Server: Unable to create item.Please refresh the page and try again");
})
return defer.promise;
}
factory.deleteItem = function (id, listname) {
var deferDelete = $q.defer();
$http({
url: currentsiteurl + "/_api/web/lists/getbytitle('" + listname + "')/items(" + id + ")",
method: "POST",
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": document.getElementById("__REQUESTDIGEST").value,
"content-Type": "application/json;odata=verbose",
"IF-MATCH": "*",
"X-Http-Method": "DELETE"
},
}).success(function (result) {
deferDelete.resolve(result);
//alert("Item deleted");
}).error(function (result, status) {
alert("Error from Server: Unable to delete item.Please refresh the page and try again");
deferDelete.reject(status);
});
return deferDelete.promise;
}
return factory;
});
app.factory('SP_AuthFactory', function ($http, $q, $location) {
var factory = {};
factory.getCurrUsergroups = function () {
var assignedusergroup = "";
var locdefer = $q.defer();
$().SPServices({
operation: "GetGroupCollectionFromUser",
userLoginName: $().SPServices.SPGetCurrentUser(),
async: false,
completefunc: function (xData, Status) {
$(xData.responseXML).find("Group").each(function () {
assignedusergroup = assignedusergroup + "\n" + $(this).attr("Name");
});
locdefer.resolve(assignedusergroup);
}
});
return locdefer.promise;
}
return factory;
});
})();
Comments
Post a Comment