简体   繁体   English

Knockout.js:高亮显示文本框中的值更改代码

[英]Knockout.js: highlight text box on value change from code

I'm using Knockout.js in my ASP.NET MVC4 project in combination with SignalR for a realtime visualization of industrial facilities. 我将ASP.NET MVC4项目中的Knockout.js与SignalR结合使用,以实现工业设施的实时可视化。 We have many pages where spans or text boxes are data-bound to a view model. 我们有许多页面,其中跨度或文本框与视图模型数据绑定。 Every 2 to 10 seconds a new value is sent from the server to the clients browser via SignalR and the value in the view model (and thereby the text in the span, div or text box input) gets updated. 每隔2到10秒,就会通过SignalR将新值从服务器发送到客户端浏览器,并且视图模型中的值(并由此更新span,div或文本框输入中的文本)。 This works very well. 这很好。 What I'm trying to achive is some kind of visual notification on the page (eg jQuery-highlight the DOM element that displays the value) whenever a value changes. 我试图实现的是每当值更改时页面上的某种视觉通知(例如,jQuery突出显示显示该值的DOM元素)。

I guess the knockout.js event-binding is close to what I need, but as far as I can see it can only pass the relevant view model item to a JS function, not the DOM element that I would need to highlight. 我猜想基因敲除事件绑定很接近我的需要,但是据我所知,它只能将相关的视图模型项传递给JS函数,而不是我需要突出显示的DOM元素。 I hope I got the idea across. 我希望我能理解。

So what I need is a way to trigger a function when the value of a text box input gets changed from "code behind" and not from manual user input. 因此,我需要一种在文本框输入的值从“落后代码”而不是手动用户输入更改时触发功能的方法。 This function should be given the concerning DOM element for further processing. 应该为该功能提供相关的DOM元素,以进行进一步处理。

My code so far: 到目前为止,我的代码:

    <table class="StatusRowList">
        <thead>
            <tr>
                <th>Parameter</th>
                <th>Value</th>
            </tr>
        </thead>
        <tbody data-bind="foreach: Items">
            <tr>
                <td><a data-bind="attr: { href: '/Archive/Show/?StationId=' + StationId() + '&DataPointId=' + DpId() }"><span data-bind="text: Text"></span></a></td>
                <td>
                    <span data-bind="text: UnitPrefix"></span>
                    <input data-bind="value: Value" />
                    <span data-bind="text: UnitSuffix"></span>
                </td>
            </tr>
        </tbody>
    </table>

The line of interest is the "input" between the two spans. 感兴趣的线是两个跨度之间的“输入”。 Thanks for help on that! 感谢您的帮助!

There is no way to distinct if a observable was set from a binding or from a ViewModel. 无法通过绑定或ViewModel来区分是否设置了可观察对象。 You will have to write a ko extender that fixes this something like 您将必须编写一个ko扩展程序来解决此问题,例如

http://jsfiddle.net/a3fFa/ http://jsfiddle.net/a3fFa/

ko.extenders.eventDriven = function(target, options) {
    var computed = ko.computed({
        read: target,
        write: target
    });
    computed.updatedFromEvent = ko.observable(false);
    computed.onEvent = function(value) {
        target(value);
        computed.updatedFromEvent(true);
    }    

    return computed;
};

edit: I did a framework for this btw, https://github.com/AndersMalmgren/Knockout.Concurrency But its more aimed to detecing when another user saves and present the conflicts to the user http://jsfiddle.net/7atZT/1/ 编辑:我为此btw, https://github.com/AndersMalmgren/Knockout.Concurrency做了一个框架,但是它的目的更多是为了确定何时其他用户保存并向用户http://jsfiddle.net/7atZT/提出冲突1 /

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

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