简体   繁体   English

敲除全局自定义绑定

[英]knockout global custom binding

The requirement is to format the amounts displayed on all the pages. 要求是格式化所有页面上显示的金额。 This is my code for custom binding. 这是我的自定义绑定代码。

(function () {
    function refresh(element, valueAccessor) {
        var val = ko.utils.unwrapObservable(valueAccessor());
        $(element).text(getCultureSpecificAmount(val));
    }
    ko.bindingHandlers.currency = {
        init: refresh,
        update: refresh
    }
})();

And this is the method which formats the amounts (not so relevant but still posting) 这是格式化金额的方法(不太相关,但仍过帐)

function getCultureSpecificAmount(number) {
var result = 0;
var regex = /[+-]?\d+(?:\.\d+)?/g;
var tempNumber = number;

if (match = regex.exec(number.toString())) {
    tempNumber = match[0];
}

result = (parseFloat(tempNumber)).toLocaleString(culture, { maximumFractionDigits: currencyDecimalDigits, minimumFractionDigits: 0 });
return (number.toString()).replace(tempNumber, result);

} }

This is from the cshtml to show how I am binding it 这是从cshtml来显示我如何绑定它

 <span style="font-weight:bold" data-bind="currency:PurchaseOrderValue"></span>

The getCultureSpecificAmount method is written in a common js. getCultureSpecificAmount方法是用通用的js编写的。 Currently I am writing the code for custom binding on each js. 目前,我正在为每个js编写用于自定义绑定的代码。 If I move this code to the common.js then it stops working. 如果我将此代码移到common.js,则它将停止工作。 Writing this code on every page makes the code look really ugly. 在每个页面上编写此代码会使代码看起来很难看。 Is there a way to define custom binding globally and use it across all pages. 有没有一种方法可以全局定义自定义绑定并在所有页面上使用它。 This is my project on knockout so I am completely clueless. 这是我的淘汰赛项目,所以我一无所知。

Here is something that works. 这是可行的。 One of the issues I found was that the if(match = regex.exec(...)) needed to move outside of the if(...) statement, but other than that, the below code is the essentially the same, so you weren't far off in getting it working. 我发现的问题之一是, if(match = regex.exec(...))需要移到if(...)语句之外,但if(match = regex.exec(...)) ,以下代码基本相同,这样您就可以开始工作了。

 function getCultureSpecificAmount(number) { var result = 0; var regex = /[+-]?\\d+(?:\\.\\d+)?/g; var tempNumber = number; var match = regex.exec(number.toString()); if (match != null) { tempNumber = match[0]; } var culture = "en-AU"; var currencyDecimalDigits = 2; result = (parseFloat(tempNumber)).toLocaleString(culture, { maximumFractionDigits: currencyDecimalDigits, minimumFractionDigits: 0 }); return (number.toString()).replace(tempNumber, result); } function refresh(element, valueAccessor) { var val = ko.utils.unwrapObservable(valueAccessor()); $(element).text(getCultureSpecificAmount(val)); } ko.bindingHandlers.currency = { init: refresh, update: refresh } var vm = { PurchaseOrderValue: ko.observable(3596.94985) }; ko.applyBindings(vm); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label>Purchase Order Total</label> <span style="font-weight:bold" data-bind="currency:PurchaseOrderValue"></span> <br/> <label>Edit Purchase Order Total</label> <input data-bind="textInput: PurchaseOrderValue" /> 

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

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