0

Why dropping and object it does't disappear right away ? I have this peace of code, which deletes object, but it drops only after refreshing the page from web.

One solution would be refresing the page automaticly after deleting, is there any other solution ?

I guess with strings works like that, when using splice(1,$index);

drop is from my factory

app.factory('Inventory', function($resource, $http) {

    return $resource('http://someweb.com/api/v1/inventory/:id', {id: "@id"},
        {
            update: {
                method: 'POST',
                params: {id: '@id'},
                isArray: false
            },
            save: {
                method: 'PUT'
            },
            query: {
                method: 'GET',
                params: {id: '@id'},
                isArray: false
            },
            create: {
                method: 'POST'
            },
            drop: {
                method: 'DELETE',
                params: {id: "@id"}
            }
        }
    );
});

delete function

$scope.deleteInv = function(id) {
    for(var i = 0; i < $scope.info.objects.length; i++){
        if($scope.info.objects[i].id == id){
            Inventory.drop({id: id});
            break;
        } 
    }
};
Davin Tryon
  • 62,665
  • 13
  • 135
  • 126
user3305175
  • 55
  • 2
  • 9

1 Answers1

0

Even though your element is removed on the server side it is not yet removed on the client.

Try doing this instead:

$scope.deleteInv = function(id) {
    var scopeObjectsLength = $scope.info.objects.length;
    for(var i = 0; i < scopeObjectsLength; i++){
        if($scope.info.objects[i].id == id){
            Inventory.drop({id: id});
            $scope.info.objects.splice(i, 1); // REMOVE the element from client
            break;
        } 
    }
};
qwertynl
  • 3,844
  • 1
  • 19
  • 43
  • ok, tnx for the tip, but seams not working with this example. Way do you use `i` and `0`? – user3305175 Feb 28 '14 at 15:04
  • @user3305175 sorry I made a typo. It should be `.splice(index, 1)` as I updated to. See the answer [here](http://stackoverflow.com/a/5767357/1022697) for the reason – qwertynl Feb 28 '14 at 15:05
  • hmm should i write `function(id,index)` ? And when i am deleting this should't I specify `index` in template `remove` – user3305175 Feb 28 '14 at 15:09