简体   繁体   English

Angular JS-对SharePoint列表的嵌套http调用

[英]Angular JS - Nested http calls to SharePoint Lists

We have a requirement on retrieving data from multiple SharePoint lists and creating a joined model to be displayed in the view. 我们需要从多个SharePoint列表中检索数据并创建要在视图中显示的联接模型。 We have two lists Employee and Department. 我们有两个列表员工和部门。 Employee list would have field as "Department" which needs to be joined with Department List's Title Field 员工列表的字段为“部门”,需要与部门列表的“标题”字段结合在一起

Below is the controller along with used services, but it is not working as expected. 以下是控制器以及使用过的服务,但无法正常工作。

Employee List data 员工清单数据

[ { "Id": 1, "Title": "1", "Name": "ABC", "Salary": 1000, "Department": "1", "ID": 1 }, { "Id": 2, "Title": "2", "Name": "DEF", "Salary": 600, "Department": "2", "ID": 2 }, { "Id": 3, "Title": "3", "Name": "GHI", "Salary": 500, "Department": "3", "ID": 3 } ] [{“ Id”:1,“ Title”:“ 1”,“ Name”:“ ABC”,“ Salary”:1000,“ Department”:“ 1”,“ ID”:1},{“ Id”: 2,“ Title”:“ 2”,“ Name”:“ DEF”,“ Salary”:600,“部门”:“ 2”,“ ID”:2},{“ Id”:3,“ Title”: “ 3”,“名称”:“ GHI”,“薪金”:500,“部门”:“ 3”,“ ID”:3}]

Department List data 部门清单数据

[ { "Id": 1, "Title": "1", "Name": "DOC", "Location": "DIJ", "ID": 1 }, { "Id": 2, "Title": "2", "Name": "DYU", "Location": "RTY", "ID": 2 }, { "Id": 3, "Title": "3", "Name": "UCV", "Location": "TYU", "ID": 3 } ] [{“ Id”:1,“ Title”:“ 1”,“ Name”:“ DOC”,“ Location”:“ DIJ”,“ ID”:1},{“ Id”:2,“ Title”: “ 2”,“名称”:“ DYU”,“位置”:“ RTY”,“ ID”:2},{“ Id”:3,“ Title”:“ 3”,“ Name”:“ UCV”, “位置”:“ TYU”,“ ID”:3}]

I am getting some [ngRepeat:dupes] error. 我收到一些[ngRepeat:dupes]错误。

(function () {
    angular.module("app")
            .controller('homeCtrl', ['$scope', 'employeeService', 'departmentService', function ($scope, employeeService, departmentService) {
                var employeevm = {};
                var employeesvm = [];
                employeeService.getAll()
                .then(function (response) {
                    $scope.employees = response.d.results;
                    for (var i = 0; i < $scope.employees.length; i++) {
                        employeevm.Id = $scope.employees[i].Title;
                        employeevm.Name = $scope.employees[i].Name;
                        employeevm.Salary = $scope.employees[i].Salary;
                        departmentService.getItem($scope.employees[i].Department)
                        .then(function (response) {
                            $scope.department = response.d.results;
                            employeevm.DepartmentName = $scope.department[0].Name;
                            employeevm.Location = $scope.department[0].Location;

                            employeesvm.push(employeevm);
                        })
                    }
                    $scope.employeesvm = employeesvm;
                })
            }]);
})();

EmployeeService.js EmployeeService.js

"use strict";
(function () {
    angular.module("app")
    .factory("employeeService", ["baseSpServices", function (baseService) {
        var listEndPoint = '/_api/web/lists';
        var getAll = function () {
            var query = listEndPoint + "/GetByTitle('Employee')/Items?$select=ID,Title,Name,Salary,Department";
            return baseService.getRequest(query);
        };
        return {
            getAll: getAll,
        };
    }]);
})();

departmentService.js departmentService.js

"use strict";
(function () {
    angular.module("app")
    .factory("departmentService", ["baseSpServices", function (baseService) {
        var listEndPoint = '/_api/web/lists';
        var getItem = function (id) {
            var query = listEndPoint + "/GetByTitle('Department')/Items?$select=ID,Title,Name,Location&$filter=Title eq " + id;
            return baseService.getRequest(query);
        }
        return {
            getItem: getItem
        };
    }]);
})();

baseSPService.js baseSPService.js

"use strict";
(function () {
    angular.module("app")
        .factory("baseSpServices", ["$http", "$q", "spContext", function ($http, $q, spContext) {
            var baseUrl = _spPageContextInfo.siteAbsoluteUrl;
            var getRequest = function (query) {
                var deferred = $q.defer();
                $http({
                    url: baseUrl + query,
                    method: "GET",
                    headers: {
                        "accept": "application/json;odata=verbose",
                        "content-Type": "application/json;odata=verbose"
                    }
                })
                    .success(function (result) {
                        deferred.resolve(result);
                    })
                    .error(function (result, status) {
                        deferred.reject(status);
                    });
                return deferred.promise;
            };
            var postRequest = function (data, url) {
                var deferred = $q.defer();
                $http({
                    url: baseUrl + url,
                    method: "POST",
                    headers: {
                        "accept": "application/json;odata=verbose",
                        "X-RequestDigest": document.getElementById("__REQUESTDIGEST").value,
                        "content-Type": "application/json;odata=verbose"
                    },
                    data: JSON.stringify(data)
                })
                    .success(function (result) {
                        deferred.resolve(result);
                    })
                    .error(function (result, status) {
                        deferred.reject(status);
                    });
                return deferred.promise;
            };
            var updateRequest = function (data, url) {
                var deferred = $q.defer();
                $http({
                    url: baseUrl + url,
                    method: "PATCH",
                    headers: {
                        "accept": "application/json;odata=verbose",
                        "X-RequestDigest": document.getElementById("__REQUESTDIGEST").value,
                        "content-Type": "application/json;odata=verbose",
                        "X-Http-Method": "PATCH",
                        "If-Match": "*"
                    },
                    data: JSON.stringify(data)
                })
                    .success(function (result) {
                        deferred.resolve(result);
                    })
                    .error(function (result, status) {
                        deferred.reject(status);
                    });
                return deferred.promise;
            };
            var deleteRequest = function (url) {
                var deferred = $q.defer();
                $http({
                    url: baseUrl + url,
                    method: "DELETE",
                    headers: {
                        "accept": "application/json;odata=verbose",
                        "X-RequestDigest": document.getElementById("__REQUESTDIGEST").value,
                        "IF-MATCH": "*"
                    }
                })
                    .success(function (result) {
                        deferred.resolve(result);
                    })
                    .error(function (result, status) {
                        deferred.reject(status);
                    });
                return deferred.promise;
            };
            return {
                getRequest: getRequest,
                postRequest: postRequest,
                updateRequest: updateRequest,
                deleteRequest: deleteRequest
            };
        }]);
})();

You haven't provided much information about what exactly is not working as expected. 您还没有提供太多有关无法正常工作的信息。

Just by viewing your code it looks like your nested then could be the problem. 只需通过查看你的代码,它看起来像你的嵌套then可能是问题。 You assign response.d.results to $scope.department , then read values from $scope.department and assign them to properties of employeevm . 您将response.d.results分配给$scope.department ,然后从$scope.department读取值并将它们分配给employeevm属性。 The problem is that the nested then runs multiple times, so the value assigned to $scope.department is going to change over time. 问题在于嵌套then将运行多次,因此分配给$scope.department值将随着时间变化。

I'd suggest not assigning anything to $scope.department at all but instead just use the response directly, as follows: 我建议根本不给$scope.department分配任何东西,而是直接使用响应,如下所示:

.then(function (response) {
    employeevm.DepartmentName = response.d.results[0].Name;
    employeevm.Location = response.d.results[0].Location;
    employeesvm.push(employeevm);
}

If that's not the problem please provide more information on what's not working. 如果这不是问题,请提供更多有关不起作用的信息。

I have modified my controller as below. 我已如下修改控制器。 I am pretty sure the issue was with handling the promises. 我很确定问题在于兑现诺言。 Any help on handling error and other performance refinements is very much helpful 对处理错误和其他性能改进的任何帮助都非常有帮助

(function () {
    angular.module("app")
            .controller('homeCtrl', ['$filter', '$scope', '$q', 'employeeService', 'departmentService', function ($filter, $scope, $q, employeeService, departmentService) {

                var departments = [];
                var employeesvm = [];

                employeeService.getAll()
                    .then(function (employees) {
                        var promises = [];
                        $scope.employees = employees.d.results;
                        angular.forEach($scope.employees, function (employee) {
                            promises.push(departmentService.getItem(employee.Department));
                        })
                        $q.all(promises).then(function (responses) {
                            departments = [];
                            angular.forEach(responses, function (response) {
                                departments.push(response.d.results[0]);
                            })

                            for (var i = 0; i < $scope.employees.length; i++) {
                                var employeevm = {};

                                employeevm.Id = $scope.employees[i].Title;
                                employeevm.Name = $scope.employees[i].Name;
                                employeevm.Salary = $scope.employees[i].Salary;
                                var department = $filter('filter')(departments, { Title: $scope.employees[i].Department });
                                employeevm.DepartmentName = department[0].Name;
                                employeevm.Location = department[0].Location;

                                employeesvm.push(employeevm);
                            }

                            $scope.employeesvm = employeesvm;
                        })
                    })


            }]);
})();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM