根据存储的字段值删除多个键

时间:2014-05-20 14:04:38

标签: goinstant goangular

我有几条带有唯一标识符的记录,我想在表单执行它的ajax后删除它。假设我的goinstant存储中有5条记录,公共字段为 xkey ,其中的值包含 123 * 。 我的问题是如何干净地删除多个密钥?任何例子都会非常有用。

Slukeheart在最佳实践中得到了正确的答案:我找到了一个有效的解决方案,但我正在向Slukeheart提供正确的答案。这是我发现的解决方案今天发布的同时工作:

$.ajax({
  url: './angtest',
  type: 'POST',
  contentType: 'application/json',
  data: JSON.stringify({ model: mymodelobj }),
  success: function (result) {
     //handleData(result);
     //remove old record entries to prevent table bloat.
  $scope.person = $goQuery('person', { xkey:  @Html.Raw(json.Encode(ViewBag.xkey1)) }, { limit: 30 }).$sync();
  $scope.person.$on('ready', function () {
    var tokill = $scope.person.$omit();
    angular.forEach(tokill, function(person,key) {
        $scope.person.$key(key).$remove();
    })
  });
  }

});

1 个答案:

答案 0 :(得分:6)

GoAngular& Angular都使用promises,它提供了一种管理异步方法调用的有效方法(如key.$remove)。 GoAngular使用Q promise库,Angular使用Q的子集,恰当地命名为$q

我刚刚简要总结了使用下面的共享 xkey 删除多个密钥,我还准备了更详细的工作plunkr

angular
  .module('TestThings', ['goangular'])
  .config(function($goConnectionProvider) {
    $goConnectionProvider.$set('https://goinstant.net/mattcreager/DingDong');
  })
  .controller('TestCtrl', function($scope, $goKey) {
    var uid = 'xkey'; // This is created dynamically in the working example

    // Create a collection or promises, each will be resolved once the associated key has been removed.  
    var removePromises = ['red', 'blue', 'green', 'purple', 'yellow'].map(function(color) {
      return $goKey('colors/' + color + '/' + uid).$remove();
    });

    // Once all of the keys have been removed, we log out the destroyed keys
    Q.all(removePromises).then(function(removedColors) {
      removedColors.forEach(function(color) {
        console.log('Removed color with key', color.context.key);
      });
    }).fail(function() {
      console.log(arguments); // Called if a problem is encountered
    });
  });