简体   繁体   English

其中一个属性没有被Knockout观察到

[英]One of the property is not being observed by Knockout observable

So I do have a knock out template and it looks like following: 所以我确实有一个淘汰模板,看起来如下:

<tbody data-bind='foreach: PrizeFulfilmentStatuses'>
            <tr >
                <td><input data-bind='value: Description' /></td> 
                <td><input data-bind='value: Order' /></td>
                <td><input type="checkbox" data-bind="checked: Editable"/></td> 
                <td> <div data-bind = "attr:{color:ColorHex },style:{backgroundcolor: ColorHex}, value: ColorHex"  class="colorpickerHolder"  style="height:30px; border: transparent; width: 30px;"></div> </td>
            </tr>
        </tbody>

Now, you can see that a lot of properties in the last TD are being assigned ColorHex. 现在,您可以看到最后一个TD中的许多属性都被分配给ColorHex。 Now, this is result of me trying to figure out something. 现在,这是我试图找出一些东西的结果。

Apparantly, all the other observables like Editable and description are two way bindings such that if I change any value on the UI then the observable is also changed and change is reflected back when I POST the data back to the server. 显然,所有其他可观察对象(如可编辑和描述)都是双向绑定,这样如果我在UI上更改任何值,那么当我将数据发送回服务器时,observable也会被更改并反映出更改。 ColorHex is not. ColorHex不是。 The value posted to the server is the original value that originally came from the server. 发布到服务器的值是最初来自服务器的原始值。 No changes are reflected here. 这里没有反映出任何变化。

following is the code that changes the properties bound to ColorHex. 以下是更改绑定到ColorHex的属性的代码。 It is a colorpicker Jquery plugin: 它是一个colorpicker Jquery插件:

self.ApplyColorPicker = function () {
            var $target = $(this);
            $(this).css("background-color", $(this).attr('color'));
            $(this).ColorPicker({
                color: $(this).attr('color'),
                onShow: function (colpkr) {
                    $(colpkr).fadeIn(500);
                    return false;
                },
                onHide: function (colpkr) {
                    $(colpkr).fadeOut(500);
                    return false;
                },
                onChange: function (hsb, hex, rgb) {
                    $target.css('backgroundColor', '#' + hex);
                    $target.attr('value', '#' + hex);
                }
            });
        };

You can see those two lines: 你可以看到这两行:

$target.css('backgroundColor', '#' + hex);
                        $target.attr('value', '#' + hex);

when those are executed, I can see in firebug that values are being changed both for "background-color" and value. 当这些被执行时,我可以在萤火虫中看到价值正在改变“背景颜色”和价值。 But when it comes to posting the value or Posts the old value NOT the updated value. 但是,当发布值或发布旧值而不是更新的值时。

Any suggestions why? 有什么建议吗?

Knockout doesn't know when you edit the value via javascript. Knockout不知道你何时通过javascript编辑值。 You'll have to manually update the KO variable using ko.dataFor($target[0]).ColorHex('#' + hex) : http://jsfiddle.net/UkJ6R/ 你必须使用ko.dataFor($target[0]).ColorHex('#' + hex)手动更新KO变量ko.dataFor($target[0]).ColorHex('#' + hex)http//jsfiddle.net/UkJ6R/

I also fixed your style binding to use backgroundColor instead and removed the other two lines in the onChange method because KO will update those attributes for you. 我还修改了样式绑定以使用backgroundColor并删除了onChange方法中的其他两行,因为KO将为您更新这些属性。

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

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