Knockout.js - ko.mapping.fromJS - 可见绑定不更新 [英] Knockout.js - ko.mapping.fromJS - Visible Binding not updating

查看:1091
本文介绍了Knockout.js - ko.mapping.fromJS - 可见绑定不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作了一个网站,每5秒,它会调用到远程数据库页面上更新值,并返回通过获取的Json呼叫MVC模式,并称之为

视图模型= ko.mapping.fromJS(模型)

我然后使用另一个get调用,并调用这个映射调用随后更新此视图模型,每5秒

 视图模型= ko.mapping.fromJS(模型,视图模型)。

的绑定是在我的HTML元素的原始模型是从数据库中检索显示在屏幕上,但正确的则当该模型没有IsVisible属性发生,即表行应设置为不可见,而另一应设置为可见。

在每次更新模式应有所不同,设置为可见,或与其他范围的文本更新沿着无形行,这部分工作,并更新显示在页面上,只是知名度不发生变化。

HTML发挥有形无形的问题,与该更新通话的JavaScript。

所有的模型变量被正确调用我不能发布该模型为公众。

 <表类=SelectionTable的cellpadding =0CELLSPACING =0>
    < TBODY数据绑定=的foreach:{数据:markets.Selections,如:'选择'}>
       < TR类=选择>
          < TD><跨度数据绑定='文字:selections.Number,可见:selections.IsVisible'>< / SPAN>< / TD>
          < TD><跨度数据绑定='文字:selections.Name,可见:selections.IsVisible'>< / SPAN>< / TD>
          < TD><跨度数据绑定='文字:selections.CurrentPrice,可见:selections.IsVisible'>< / SPAN>< / TD>
          < TD><跨度数据绑定='文字:selections.OpeningPrice,可见:selections.IsVisible'>< / SPAN>< / TD>
       < / TR>
    < / TBODY>
< /表><脚本类型=文/ JavaScript的>
    VAR视图模型;
    VAR自我;    VAR getUpdates =的setInterval(函数(){
        $ .getJSON(
            /首页/ GET,{},
            功能(模型){
                视图模型= ko.mapping.fromJS(模型,视图模型);
            });
    },5000);    $(文件)。就绪(
        功能(){
            $ .getJSON(
                /首页/ GET,{},
                功能(模型){
                    视图模型= ko.mapping.fromJS(模型);
                    bindViewModel();
                });
        });    功能bindViewModel(){
        ko.applyBindings(视图模型);
    }
< / SCRIPT>


解决方案

我发现你有时需要提供一个空的映射更新视图模型时:

  ko.mapping.fromJS(型号{}视图模型);

如果做不到这一点,输出 selections.IsVisible 的值,并确保它是可以解析为真或假的格式。

I am working on a website that updates values on the page every 5 seconds, it calls to a remote database and returns a MVC model through a Get Json call, and call

viewModel = ko.mapping.fromJS(model).

I am then updating this view model every 5 seconds using another Get call and call this mapping call then

 viewModel = ko.mapping.fromJS(model, viewModel). 

The bindings are correct on my HTML elements as the original model that is retrieved from the database is displayed on the screen but then when the IsVisible property on the model nothing happens, ie the table row should be set to invisible, and another should be set to visible.

On each update the model should be different, with rows set to visible or invisible along with other span's text updating, this part is working, and updates are showing on the page, just the visibility is not changing.

HTML exert of the visible invisible problem, with Javascript of the update call.

All variables from the Model are correctly called I cannot post the model up for the public.

<table class="SelectionTable" cellpadding="0" cellspacing="0">
    <tbody data-bind="foreach: { data: markets.Selections, as: 'selections' }">
       <tr class="Selection">
          <td><span data-bind='text: selections.Number, visible: selections.IsVisible'></span></td>
          <td><span data-bind='text: selections.Name, visible: selections.IsVisible'></span></td>
          <td><span data-bind='text: selections.CurrentPrice, visible: selections.IsVisible'></span></td>
          <td><span data-bind='text: selections.OpeningPrice, visible: selections.IsVisible'></span></td>
       </tr>
    </tbody>
</table>

<script type="text/javascript">
    var viewModel;
    var self;

    var getUpdates = setInterval(function () {
        $.getJSON(
            "/Home/Get", {},
            function (model) {
                viewModel = ko.mapping.fromJS(model, viewModel);
            });
    }, 5000);

    $(document).ready(
        function () {
            $.getJSON(
                "/Home/Get", {},
                function (model) {
                    viewModel = ko.mapping.fromJS(model);
                    bindViewModel();
                });
        });

    function bindViewModel() {
        ko.applyBindings(viewModel);
    }
</script>

解决方案

I find that you sometimes need to provide an empty mapping when updating a viewmodel:

ko.mapping.fromJS(model, {}, viewModel);

Failing that, output the value of selections.IsVisible and make sure that it is in a format that can resolve to true or false.

这篇关于Knockout.js - ko.mapping.fromJS - 可见绑定不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆