繁体   English   中英

接受 KO 数字扩展中的小数位

[英]Accept decimal places in KO numeric extension

我已经实现了 Knockout live 1 示例中显示的扩展器 - https://knockoutjs.com/documentation/extenders.html

ko.extenders.precision = function (target, precision) {
  //create a writable computed observable to intercept writes to our observable
  var result = ko
    .pureComputed({
      read: target, //always return the original observables value
      write: function (newValue) {
        var current = target(),
          roundingMultiplier = Math.pow(10, precision),
          newValueAsNum = isNaN(newValue) ? 0 : +newValue,
          valueToWrite =
            Math.round(newValueAsNum * roundingMultiplier) / roundingMultiplier;

        //only write if it changed
        if (valueToWrite !== current) {
          target(valueToWrite);
        } else {
          //if the rounded value is the same, but a different value was written, force a notification for the current field
          if (newValue !== current) {
            target.notifySubscribers(valueToWrite);
          }
        }
      },
    })
    .extend({ notify: "always" });

  //initialize with current value to make sure it is rounded appropriately
  result(target());

  //return the new computed observable
  return result;
};

model视图如下:

model.gwaioDifficultySettings = {
  minionMod: ko.observable(0).extend({
    precision: 3,
    rateLimit: { timeout: 500, method: "notifyWhenChangesStop" },
  }),
};

它可以通过 HTML 输入字段访问:

  <div>
    <input
      type="number"
      style="width: 50px; padding-bottom: 0px;"
      data-bind="textInput: model.gwaioDifficultySettings.minionMod"
    />
    <span style="margin-left: 6px;"></span>
    <loc>Minion Modifier</loc>
    <span
      class="info_tip"
      data-bind="tooltip: '!LOC:Mandatory Minions + Star Distance * Minion Modifier = number of additional enemy Commanders.'"
      >?</span
    >
  </div>

但是,我必须添加rateLimit: { timeout: 500, method: "notifyWhenChangesStop" } ,否则我发现一旦值为 integer,该字段将删除小数点,然后它将阻止任何尝试输入又是小数点。

在实际示例中似乎并非如此,我想知道我哪里出错了,或者仅仅是因为我使用 Knockout 3 和 Chromium 28 的环境。

我将https://gist.github.com/gnab/7579584中的一些代码添加到我的扩展器中:

        if (_.isString(newValue)) {
          newValue = newValue.replace(",", ".");

          newValue = parseFloat(newValue);
          if (!isNaN(newValue)) {
            target(newValue);
          }
        }

这导致:

ko.extenders.precision = function (target, precision) {
  //create a writable computed observable to intercept writes to our observable
  var result = ko
    .pureComputed({
      read: target, //always return the original observables value
      write: function (newValue) {
        if (_.isString(newValue)) {
          newValue = newValue.replace(",", ".");

          newValue = parseFloat(newValue);
          if (!isNaN(newValue)) {
            target(newValue);
          }
        }
        var current = target(),
          roundingMultiplier = Math.pow(10, precision),
          newValueAsNum = isNaN(newValue) ? 0 : +newValue,
          valueToWrite =
            Math.round(newValueAsNum * roundingMultiplier) / roundingMultiplier;

        //only write if it changed
        if (valueToWrite !== current) {
          target(valueToWrite);
        } else {
          //if the rounded value is the same, but a different value was written, force a notification for the current field
          if (newValue !== current) {
            target.notifySubscribers(valueToWrite);
          }
        }
      },
    })
    .extend({ notify: "always" });

  //initialize with current value to make sure it is rounded appropriately
  result(target());

  //return the new computed observable
  return result;
};

到目前为止,在我所有的测试中,这似乎都按要求执行。 如果输入一个小数点,它会留下一个可见的悬挂小数点,但这似乎不会产生任何负面影响。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM