具有约束的基因敲除

具有约束的基因敲除

本文介绍了具有约束的基因敲除值绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Knockoutjs来实现购物车.要求之一是允许客户更改物品数量.所以我自然会这么做

I am using knockoutjs to implement a shopping cart.One of the requirements is to allow customers to change quantity of item. So I naturally did

<input type="text" data-bind="value: quantity"/>

在我的查看模式下,我有一个项目ViewModel定义为

in my view mode I have an Item ViewModel defined as

Item = function(){
    ...
    _self.quantity = ko.observable(1);
    ...
}

但是,还有另一个要求,即购物车中的总金额应有上限.

But then there is another requirement that the total dollar amount in the shopping cart should have an upper limit.

我可以使用jquery截获"change"事件并在其中添加一些逻辑,但是我想知道是否有任何本机的敲除方法可以执行类似

I can use jquery to intercept the "change" event and add some logic there, but I am wondering if there is any native knockout way to do something like

<input type="text" data-bind="value: quantity where quantity*uniprice <= maxDollarAmount"/>

推荐答案

您应使用计算的剔除可观察到的:

ViewModel:

ViewModel:

Item = function(){
    ...
    _self.quantity = ko.observable(1);
    _self.attemptedQuantity = ko.computed({
        read: function () {
            return _self.quantity();
        },
        write: function (value) {
            if (value * uniprice <= maxDollarAmount) {
                self.quantity(value);
            } else {
                // alert or something
            }
        }
    });
    ...
}

HTML:

<input type="text" data-bind="value: attemptedQuantity"/>

这篇关于具有约束的基因敲除值绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 04:47