我目前有一个非常简单的应用程序,它最初调用一个 php 文件来获取数据,然后遍历数据集并创建一个表。在表中,我有一个“启用/禁用”按钮,单击该按钮将更新模型,然后将其推送到持久层。我遇到的问题是,虽然我能够更新模型来很好地更新视图,但我无法弄清楚如何让它的持久层部分工作。我为服务“启用/禁用”添加了两个自定义函数以反映按钮单击事件,但我不确定我是否朝着正确的方向前进,但我的代码如下。
视图部分:
<table class="table table-hover">
<thead><tr><th>Name</th><th>Username</th><th>Details</th><th>Facility</th><th>Last Login</th><th>Days from last login</th></tr></thead>
<tbody>
<tr ng-class="{'error':user._accountDisabled,'success':user._accountDisabled==false}" ng-repeat="user in users | filter:query | orderBy:orderProp">
<td>{{user._firstName}} {{user._lastName}}</td>
<td>{{user._userName}}</td>
<td><a class="btn btn-primary" href="#/userExceptions/{{user._userName}}">Details</a></td>
<td>{{user._facilityName}}</td>
<td>{{user._hrLastLogon}}</td>
<td>{{user._daysLastLogon}}</td>
<td>
<ng-switch on="user._accountDisabled">
<button ng-switch-when=true class="btn btn-primary" ng-click="enable(user)">Enable</button>
<button ng-switch-when=false class="btn btn-danger" ng-click="disable(user)">Disable</button>
</ng-switch>
</td>
</tr>
</tbody>
</table>
持久化的自定义服务:
angular.module('userServices', ['ngResource']).
factory('User', function($resource) {
return $resource('userActions.php', {}, {
query: {method: 'GET', params: {userName: 'userName'}, isArray: true},
enable: {method: 'GET',params: {action: 'enable', userName: 'userName'}}},
disable: {method: 'GET', params: {action: 'disable', userName: 'userName'}}
});
});
最后是控制器:
function UserExceptionsCtrl($scope, User) {
$scope.users = User.query();
$scope.orderProp = '_firstName';
$scope.enable = function(user) {
$scope.user = user;
$scope.user._accountDisabled = false;
$scope.user.$save();
User.enable({userName:user._userName});
};
$scope.disable = function(user) {
$scope.user = user;
$scope.user._accountDisabled = true;
$scope.user.$save();
User.disable({action: 'disable', userName: self._userName});
};
}
编辑按要求的服务器端代码:
useractions 文件处理请求并创建映射器以检索用户对象。它从用户对象更新必要的属性并将其保存在持久层中。
用户操作.php
$username = (isset($_REQUEST['userName']) ? $_REQUEST['userName'] : '');
$action = (isset($_REQUEST['action']) ? $_REQUEST['action'] : '');
require 'library/autoloader/src/autoload.php';
try {
$ADUserMapper = new UserMapper(new LDAPAdapter());
switch ($action) {
case 'enable':
$ADUserEditMapper = new UserMapper(new LDAPAdapter());
$user = $ADUserEditMapper->findByUsername($username);
if ($user) {
$user->enableADAccount();
$ADUserEditMapper->updateUAC($user);
}
break;
case 'disable':
$ADUserEditMapper = new UserMapper(new LDAPAdapter());
$user = $ADUserEditMapper->findByUsername($username);
if ($user) {
$user->disableADAccount();
$ADUserEditMapper->updateUAC($user);
}
break;
default:
$adapter = new PdoAdapter();
$employeeDBMapper = new EmployeeMapper($adapter);
$ADUsers = $ADUserMapper->findMultipleUsers(array('objectClass' => 'user'), "OU=Users,DC=domain,DC=com", TRUE);
$exceptions = array();
foreach ($ADUsers as $user) {
$employee = $employeeDBMapper->findByUserName($user->userName);
if (!$employee) {
array_push($exceptions, $user);
}
}
$result = array();
foreach ($exceptions as $user) {
array_push($result, $user->getExceptionData());
}
echo json_encode($result);
break;
}
} catch (Exception $e) {
echo json_encode(array('error' => true, 'errorMessage' => $e->getMessage()));
}