如何为默认的敲除绑定创建包装函数

sub*_*nid 12 javascript data-binding knockout.js

我正在展示一个巨大的表格结构与淘汰赛.用户可以通过单击行上的复选框来删除行:

data-bind="checked: row.removed"
Run Code Online (Sandbox Code Playgroud)

问题是必须在单击时重新呈现表,在慢速计算机/浏览器上需要一到两秒 - 复选框在表呈现后更改其状态,因此UI感觉无响应.我想创建一个包装函数,它执行与默认检查绑定相同的操作,但另外显示一个加载器符号 - 然后在检查的绑定完成其工作后再次隐藏它.就像是:

ko.bindingHandlers.checkedWithLoader = {
    update: function(element, valueAccessor, allBindings) {
        loader.show();
        // call knockout's default checked binding here
        loader.hide();
    }
};
Run Code Online (Sandbox Code Playgroud)

有可能吗?还有更好的选择吗?

use*_*291 7

如何访问自定义绑定中的其他绑定

你可以使用ko.applyBindingsToNode:

ko.applyBindingsToNode(element, { checked: valueAccessor() })
Run Code Online (Sandbox Code Playgroud)

Knockout的源主动公开此方法(此处)并在其自己的文档页面(此处)的示例中引用它.

尽管处理缓慢的渲染,它可能无法解决您的问题...

处理缓慢的更新

您还可以在viewmodel中创建一个额外的图层,以构建加载功能:

this.checked = ko.observable(false);

this.isLoading = ko.observable(false);
this.showLargeAndSlowTable = ko.observable(false);

this.checked.subscribe(function(isChecked) {
  this.isLoading(true);
  this.showLargeAndSlowTable(isChecked);
  this.isLoading(false);
}, this);
Run Code Online (Sandbox Code Playgroud)

您需要绑定ifwith绑定绑定showLargeAndSlowTable,并将复选框值绑定到checked.

在某些情况下,您可能需要在设置loadingobservable和注入大型数据集之间强制重新绘制.否则,淘汰赛和浏览器可以将这些更新捆绑到一个帧中.

您可以通过将实现这一目标showLargeAndSlowTable,并isLoading(false)在一setTimeout,或通过使用延迟/节流额外观察到触发后,工作isLoading的变化已给定的时间来呈现:

function AppViewModel() {
    var self = this;
    
    // The checkbox value that triggers the slow UI update
    this.showLargeTable = ko.observable(false);
    
    // Checkbox change triggers things
    this.showLargeTable.subscribe(updateUI)
    
    // Indicates when we're loading:
    this.working = ko.observable(false);
    this.delayedWorking = ko.pureComputed(function() {
      return self.working();
    }).extend({ throttle: 10 });
    
    // Instead of directly subscribing to `working`, we
    // subscribe to the delayed copy
    this.delayedWorking.subscribe(function(needsWork) {
      if (needsWork) {
        doWork();
        self.working(false);
      }
    });
    
    function updateUI(showTable) {
      if (showTable) {
        self.working(true); // Triggers a slightly delayed update
      } else {
        self.data([]);
      }
    }
    
    // Some data from doc. page to work with
    function doWork() {
      // (code only serves to mimic a slow render)
      for (var i = 0; i < 1499; i++) {
          self.data([]);
          self.data(data.reverse());
      }
    };
    
    var data = [
        { name: 'Alfred', position: 'Butler', location: 'London' },
        { name: 'Bruce', position: 'Chairman', location: 'New York' }
    ];
    
    // Some data to render
    this.data = ko.observableArray([]);
    
}


ko.applyBindings(new AppViewModel());
Run Code Online (Sandbox Code Playgroud)
.is-loading {
  height: 100px;
  background: red;
  display: flex;
  align-items: center;
  justify-content: center;
}

.is-loading::after {
  content: "LOADING";
  color: white;
  
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

<label>
  <input type="checkbox" data-bind="checked: showLargeTable, disable: working">Show slowly rendered table
</label>

<table data-bind="css: { 'is-loading': working }">
  <tbody data-bind="foreach: data">
    <tr>
      <td data-bind="text: name"></td>
      <td data-bind="text: position"></td>
      <td data-bind="text: location"></td>
    </tr>
  </tbody>
</table>

<em>Example based on the <a href="http://knockoutjs.com/documentation/deferred-updates.html">knockout docs</a></em>
Run Code Online (Sandbox Code Playgroud)