【问题标题】:Removing multiple keys based on a stored field value根据存储的字段值删除多个键
【发布时间】:2014-05-20 14:04:37
【问题描述】:

我有几条带有唯一标识符的记录,我想在表单执行它的 ajax 发布后删除这些记录。假设我的即时存储中有 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();
    })
  });
  }

});

【问题讨论】:

    标签: goinstant goangular


    【解决方案1】:

    GoAngular 和 Angular 都使用了 Promise,它提供了一种管理异步方法调用的有效方式(如 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
        });
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-18
      • 1970-01-01
      • 2017-05-02
      • 2017-11-06
      • 2017-03-03
      • 2013-07-13
      相关资源
      最近更新 更多