restful architecture - AngularJS $resource | don't update view until promise resolved -
i wanting use $resource make restful update on our server view get's updated user clicks when not update until server sends response. using controller service layer uses rest resource.
template:
<tbody> <tr ng-repeat="user in users"> <td><button class="radius tiny button" ng-click="toggleuseractive(user)" ng-bind="user.active ? 'disable user' : 'enable user'"> </button> </td> <tr> </tbody>
controller:
angular.module('admin', ['admin.adminservice']) .controller('adminctrl', function ($scope, adminservice) { $scope.toggleuseractive = function(user){ adminservice.toggleuserdisabled(user).then(function(response){ // want update happen here user.active = response; }); }; });
service:
angular.module('admin.adminservice', ['rest']) .service('adminservice', function adminservice(userrest){ this.toggleuserdisabled = function(user) { user.active = !user.active; return userrest.update(user).$promise.then(function(result){ return result.active; }); }; });
rest service:
angular.module('rest', ['config','ngresource']) .factory('restendpoint', function (apiendpoint) { return { users: apiendpoint + '/admin/users/:username' }; }) .factory('userrest', function ($resource, restendpoint){ return $resource(restendpoint.users, {username:'@username'},{ update: { method: 'put' } }); });
how can code view not update until response has made back?
you use in view
ng-show="user.active"
which show once $scope updated new data.
Comments
Post a Comment