利用beforeMove / afterMove没有约束力?(utilizing beforeMov

2019-10-17 09:47发布

我需要在项目中的移动检测observableArray 。 在我目前的版本(2.1.0),我通过调用实现这一目标setTimeout上的所有删除事件和等待,看看add事件紧跟在其高跟鞋:

  var delayed = [];
  var key; /* obtained by comparing original observableArray with updated list and iterating all differences */

  if( /** record exists in original list but not new list ) {
     // it was deleted, we don't immediately fire a notification (it may get re-inserted in a moment)
     delayed[key] = setTimeout(function() { sendDeleteNotification(key); }, 0);
  }
  else if( /** record exists in new list but not original */ ) {
     if( delayed[key] ) { 
        // it was deleted and immediately re-added (it's a move)
        clearTimeout(delayed[key]); 
        sendMoveNotification( key );
     }
     else {
        // it was added
        sendAddedNotification( key );
     }
  }

在淘汰赛2.2,我看到了beforeMove和afterMove新事件 。 但是,我找不到任何方式利用它们编程。

最终的目标是要能够反映在立即数据库客户端的变化,但并未显示出添加/代替移动事件的删除。 A记录向上或向下移动在列表中删除,然后新增不应被标记。

可以在新的beforeMove / afterMove绑定在JavaScript中直接使用(如事件模型?)改善呢?

干杯,

Answer 1:

我使用的答案是基于这个问题,所以和这个的jsfiddle 。

beforeMoveafterMove功能可以通过一个简单的订阅来访问:

var obs = ko.observableArray();
obs.subscribe(function( arrayOfValues ) {
   console.log('moving', arrayOfValues);
}, undefined, 'beforeMove');

obs.subscribe(function( arrayOfValues ) {
   console.log('moved', arrayOfValues);
}, undefined, 'afterMove');

然而,事实证明,因为它返回更改索引数组中的所有元素,这不是特别有用。 举例来说,如果我从列表中删除记录,我得到了一个删除后,每一个记录的事件感动。

所以,我会超时被坚持,直到我找到更好的东西。



文章来源: utilizing beforeMove/afterMove without a binding?