Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is this resource not updating the view after using $delete method

Tags:

angularjs

In my angular app I have a controller as follows:

function TemplateListControl($scope, TemplateService){
    $scope.templates = TemplateService.get(); // Get objects from resource

    // Delete Method
    $scope.deleteTemplate = function(id){
        $scope.templates.$delete({id: id});
    }
}

Within the view I have a table thats bound to templates model. as follows:

<table ng-model="templates">
    <thead>
        <tr>
            <th style="width:40%">Title</th>
            <th style="width:40%">controls</th>
        </tr>
    <thead>
    <tbody>
        <tr ng-repeat="template in templates">
            <td>
                <!-- Link to template details page -->
                <a href="#/template/[[template.id]]">
                    [[template.title]]
                </a>
            </td>
            <td>
                <!-- Link to template details page -->
                <a class="btn btn-primary btn-small"
                   href="#/template/[[template.id]]">Edit
                </a>
                <!-- Link to delete this template -->
                <a class="btn btn-primary btn-small"
                   ng-click="deleteTemplate(template.id)">Delete
                </a>
            </td>
        </tr>
    </tbody>
</table>

Now when I click on the delete link in the above template, It calls the deleteTemplate method and a successful DELETE call is made to the REST api. But the view does not get updated until it is refreshed and $scope.templates = TemplateService.get(); is initiated again. What am I doing wrong?

like image 803
Amyth Avatar asked Mar 15 '13 10:03

Amyth


2 Answers

I prefer using promises instead of callback. Promises are the new way to handle asynchronous processes. You can inspect the result using a a promise right after it came back from the server.

//Controller
myApp.controller('MyController',
    function MyController($scope, $log, myDataService) {

 $scope.entities = myDataService.getAll();
 $scope.removeEntity = function (entity) {       
        var promise = myDataService.deleteEntity(entity.id);
        promise.then(
            // success
            function (response) {
                $log.info(response);
                if (response.status == true) {
                    $scope.entities.pop(entity);
                }
            },
            // fail
            function (response) {
                $log.info(response);
                // other logic goes here
            }
        );
    };
 });

 //DataService
 myApp.factory('myDataService', function ($log, $q, $resource) {

return {
    getAll: function () {
        var deferred = $q.defer();
        $resource('/api/EntityController').query(
            function (meetings) {
                deferred.resolve(meetings);
            },
            function (response) {
                deferred.reject(response);
            });

        return deferred.promise;
    },

    deleteEntity: function (entityId) {
        var deferred = $q.defer();
        $resource('/api/EntityController').delete({ id: entityId},
            function (response) {
                deferred.resolve(response);
            },
            function (response) {
                deferred.reject(response);
            });

        return deferred.promise;
    }
   };
});

//Web API Controller
public class MeetingController : BaseApiController
{
   // .... code omited 

   public OperationStatus Delete(int entityId)
    {
        return _repository.Delete(_repository.Single<MyEntity>(e => e.EntityID == entityId));
    }
}

Note: $log, $q, $resource are built in services. Hope it helps :)

like image 114
Leo Avatar answered Nov 10 '22 18:11

Leo


You also have to update client side so modify your source code as below

 ng-click="deleteTemplate($index)">


$scope.delete = function ( idx ) {
  var templateid = $scope.templates[idx];

  API.Deletetemplate({ id: templateid.id }, function (success) {
    $scope.templates.splice(idx, 1);
  });
};
like image 23
Ajay Beniwal Avatar answered Nov 10 '22 18:11

Ajay Beniwal