最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Deleting entry with Restangular - Stack Overflow

programmeradmin1浏览0评论

I am using Restangular in my AngularJS app. I have a table with a delete link for each item. I would like to delete the item and have the row automatically removed. But as things are it only deletes from DB. How can I refactor things so that it the DOM is updated automatically?

// The controller
angular.module('myApp').controller('ManageCtrl', function($scope, Restangular) {

  $scope.delete = function(e) {
     Restangular.one('product', e).remove();
  };

  Restangular.all('products').getList({}).then(function(data) {
    $scope.products = data.products;
    $scope.noOfPages = data.pages;
  });
});


 // The view
 <li ng-repeat="product in products">
   <a href="#" ng-click="delete(sheet._id)"></a>
  </li>

I would also love to find an example of this - even with Angular resource. All the admin/data table demos seem to work from static data.

I am using Restangular in my AngularJS app. I have a table with a delete link for each item. I would like to delete the item and have the row automatically removed. But as things are it only deletes from DB. How can I refactor things so that it the DOM is updated automatically?

// The controller
angular.module('myApp').controller('ManageCtrl', function($scope, Restangular) {

  $scope.delete = function(e) {
     Restangular.one('product', e).remove();
  };

  Restangular.all('products').getList({}).then(function(data) {
    $scope.products = data.products;
    $scope.noOfPages = data.pages;
  });
});


 // The view
 <li ng-repeat="product in products">
   <a href="#" ng-click="delete(sheet._id)"></a>
  </li>

I would also love to find an example of this - even with Angular resource. All the admin/data table demos seem to work from static data.

Share Improve this question asked Aug 30, 2013 at 2:06 cyberwombatcyberwombat 40.1k41 gold badges184 silver badges267 bronze badges 3
  • Can you check in the code if item gets removed from $scope.products collection? – Chandermani Commented Aug 30, 2013 at 3:23
  • it doesn't. I'm thinking I have to delete from both the resource and the scope.products. I guess I am looking for a way to not have to do so - but there may not be. – cyberwombat Commented Aug 30, 2013 at 3:27
  • You can always look at the restangular source and verify :) – Chandermani Commented Aug 30, 2013 at 3:32
Add a comment  | 

2 Answers 2

Reset to default 19

According to Restangular https://github.com/mgonto/restangular#restangular-methods they mention that you should use the original item and run an action with it, so in your html code you should:

 <li ng-repeat="product in products">
   <a href="#" ng-click="delete(product)"></a>
</li>

Then in your controller:

 $scope.delete = function( product) {
    product.remove().then(function() {
      // edited: a better solution, suggested by Restangular themselves
      // since previously _.without() could leave you with an empty non-restangular array
      // see https://github.com/mgonto/restangular#removing-an-element-from-a-collection-keeping-the-collection-restangularized

      var index = $scope.products.indexOf(product);
      if (index > -1) $scope.products.splice(index, 1);
   });
 };

Notice they use the underscore.js without which will remove the element from the array. I guess that if they post that example in their readme page that means the .remove() function doesn't remove the original item from the collection. This makes sense, since not every item you remove you want removed from the collection itself.

Also, what happens if the DELETE $HTTP request fails? You don't want to remove the item then, and you have to make sure to handle that problem in your code.

In my case the above didn't quite work. I had to do the following:

$scope.changes = Restangular.all('changes').getList().$object;

    $scope.destroy = function(change) {
        Restangular.one("changes", change._id).remove().then(function() {
            var index = $scope.changes.indexOf(change);
            if (index > -1) $scope.changes.splice(index, 1);
        });
    };
发布评论

评论列表(0)

  1. 暂无评论