knockout-sortable issue #199

by fastfasterfastest

HTML

<script src="https://knockoutjs.com/downloads/knockout-3.5.0.debug.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout-sortable/1.1.1/knockout-sortable.js"></script>
<ul data-bind="sortable: { data: [1,2,3] }">
   <li data-bind="text: $data"></li>
</ul>

JavaScript

var applyKnockoutIssue2446Commits = true;

if (applyKnockoutIssue2446Commits) {
  //https://github.com/knockout/knockout/issues/2446
  //https://github.com/knockout/knockout/commit/0f0cb8e23fa3f01a9f845cb539f2b6521322664f
  //https://github.com/knockout/knockout/commit/a93358fb9e2e2ef285985df2015a442ad83fd09a
  (function() {

    var templateComputedDomDataKey = ko.utils.domData.nextKey();

    function disposeOldComputedAndStoreNewOne(element, newComputed) {
      var oldComputed = ko.utils.domData.get(element, templateComputedDomDataKey);
      if (oldComputed && (typeof(oldComputed.dispose) == 'function'))
        oldComputed.dispose();
      ko.utils.domData.set(element, templateComputedDomDataKey, (newComputed && (!newComputed.isActive || newComputed.isActive())) ? newComputed : undefined);
    }

    var cleanContainerDomDataKey = ko.utils.domData.nextKey();
    ko.bindingHandlers['template'] = {
      'init': function(element, valueAccessor) {
        // Support anonymous templates
        var bindingValue = ko.utils.unwrapObservable(valueAccessor());
        if (typeof bindingValue == "string" || 'name' in bindingValue) {
          // It's a named template - clear the element
          ko.virtualElements.emptyNode(element);
        } else if ('nodes' in bindingValue) {
          // We've been given an array of DOM nodes. Save them as the template source.
          // There is no known use case for the node array being an observable array (if the output
          // varies, put that behavior *into* your template - that's what templates are for), and
          // the implementation would be a mess, so assert that it's not observable.
          var nodes = bindingValue['nodes'] || [];
          if (ko.isObservable(nodes)) {
            throw new Error('The "nodes" option must be a plain, non-observable array.');
          }

          // If the nodes are already attached to a KO-generated container, we reuse that container without moving the
          //...