Knockout在html中刷新observableArray

Tzv*_*nov 6 jquery-mobile knockout.js cordova

我正在使用带有PhoneGap的Jquery Mobile在Android上使用Knockout来加载数据.

我正确地获取数据并将其加载到HTML页面上,相应地我对标签上的每个数据绑定.

当我想刷新数据时,它就不会这样做.它只返回一个没有绑定数据的HTML或者找不到dom异常.

我的applyBinding发生在页面的pagecreate事件中.

我发布了一个简单的例子来描述我的SkyDrive上的问题 - http://sdrv.ms/LpUdLt 这是一个重现问题的公开示例.viewmodel包含一个包含数组的数组.用randomal值刷新.尝试在jquery mobile中重新加载页面,通过按下导航栏按钮更改页面重新加载新数据失败,dom对象错误.我确实不同意我不应该在每个页面创建VM的实例,只是找不到实现它的方法,这样数据就会在HTML上重新呈现.

   //indexPage.js
   var wAViewModelInst ;
   var viewPageIndexContent;
   $('#pageIndex').live('pagecreate', function (event) { 
        viewPageIndexContent = document.getElementById("pageIndexContent");
        wAViewModelInst = new WAViewModel(true);
        ko.applyBindings(wAViewModelInst, viewPageIndexContent);

        waHeaderVM.refreshContentData = function () {

              // wAViewModelInst.updateRowList();
              // ko.cleanNode(viewPageIndexContent);
              // viewPageIndexContent = document.getElementById("pageIndexContent");
              //wAViewModelInst = new WAViewModel(true);
             //ko.applyBindings(wAViewModelInst, viewPageIndexContent);
              $.mobile.changePage("index.html", { allowSamePageTransition: true, reloadPage: true });
              $.mobile.hidePageLoadingMsg();
       }
   }

    //WAViewModel
    self.WARowList = ko.observableArray();
    self.updateRowList = function () {
        self.WARowList(self.GetWA());
    }
    //tried the exteding 
    //ko.observableArray.fn.WARowListUpdate = function () {
    //    //create a sub-observable
    //    this.hasItems = ko.observable();

    //    //update it when the observableArray is updated
    //    this.subscribe(function (newValue) {
    //        this.hasItems(newValue && newValue.length ? true : false);
    //    }, this);

    //    //trigger change to initialize the value
    //    this.valueHasMutated();

    //    //support chaining by returning the array
    //    return this;
    //};            
Run Code Online (Sandbox Code Playgroud)

有没有办法在第一次渲染后更新html?

添加html代码:

<div id="pageIndex" data-role="page" data-transition="flip" data-theme="e" data-dom-cache="true">
    <div id="indexHeader" data-role="header" data-theme="e">
        <div data-role="navbar" data-iconpos="right">
            <ul>
                <li><a href="login.html" data-role="tab" data-icon="back" data-bind="click: loadingHandler"
                    class="brighter-text">????</a></li>
                <li><a href="#" data-role="tab" data-icon="refresh" data-bind ="click: refreshContentData" >????</a></li>
                <li><a href="researchEvent.html" data-role="tab" data-icon="check" id="navBarResearchEventPage"   data-bind="click: loadResearchEvent" class="brighter-text">????? ???</a></li>
                <li><a href="#" data-role="tab" data-icon="grid" class="ui-btn-active brighter-text">
                    ????? ?????</a></li>
            </ul>
        </div>
    </div>
    <div id="pageIndexContent" data-role="content" data-theme="e" style="padding-bottom: 52px;
        height: 570px;">
        <h2 data-bind="text:Title" class="brighter-text" style="font-size:22pt;">
        </h2>
        <div data-bind="foreach: WARowList" style="width: 99%; text-align: center">
            <div>
                <table style="float: right; width: 20%; height: 60px;" cellpadding="0" cellspacing="0">
                    <tr data-bind="visible : FirstRow " style="height: 31px;">
                        <th class="AlignedHeader">
                            <label>
                                ??? / ?????</label>
                        </th>
                    </tr>
                    <tr data-bind="style: { backgroundColor: Odd() ? '#8CC63F' : '#AFC493' }">
                        <td style="width: 20%;" data-bind="style: { backgroundColor: IsNew() ? 'yellow' : 'transparent' }">
                            <input  type="button" data-bind="click: ShowSampleDetails, value: ShilutTime , jQueryButtonUIEnableDisable:$data"
                                data-icon="plus" data-iconpos="right"/>
                        </td>
                    </tr>
                </table>
                <table style="height: 60px; width: 80%; background-color: #8CC63F;" data-bind="style: { backgroundColor: Odd() ? '#8CC63F' : '#AFC493' }"
                    cellpadding="0" cellspacing="0">
                    <thead data-bind="if : FirstRow">
                        <tr data-bind="foreach: CellList">
                            <th class="AlignedHeader">
                                <label data-bind="text: Date">
                                </label>
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr data-bind="foreach: CellList">
                            <td style="width: 11.5%;">
                                <div data-bind="visible:IsPopulated ">
                                    <div data-bind="visible: HasDrivers">
                                        <input type="button" data-role="button" data-bind="click: ShowBusDriverList.bind($data , $root) , jQueryButtonUIEnableDisable: $data "
                                            data-icon="search" data-iconpos="right" value="?????" class="brighter-text" />
                                    </div>
                                    <div data-bind="visible: !HasDrivers()">
                                        <input type="button" data-role="button" id="btnNoDriver" disabled="disabled" data-icon="info"
                                            data-iconpos="right" value="??? ???" class="brighter-text" />
                                    </div>
                                </div>
                                <div data-bind="visible: !IsPopulated">
                                </div>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
Run Code Online (Sandbox Code Playgroud)

等等 ..

GetWA函数返回warow列表的observableArray.它是第一次麻烦重新渲染dom对象时工作.dom元素被ko污染并失败..

我试过路飞的答案:

var lVM = new loginViewModel();
var  footerViewModelLogin = {
        IsOnline: ko.observable(globalContext.Network()),
        IsSync: ko.observable(globalContext.Sync())
    };
$('#login').live('pagecreate', function (event) {


     viewLoginContent = document.getElementById("loginContent");

    ko.applyBindingsToNode(viewLoginContent, lVM);


    viewLoginFooter= document.getElementById("footerLogin");

    ko.applyBindingsToNode(viewLoginFooter, footerViewModelLogin);


});


$('#login').live('pagehide', function (event, ui) {
    $.mobile.hidePageLoadingMsg();

});



function loginViewModel() {
    var self = this;

    try {

        self.userName = ko.observable("");
        self.password = ko.observable("");
        self.message = ko.observable("");

        self.CleanGlobalContext = function () {
          ...

        };

        self.Validate = function () {

            ...

        };
    }
    catch (e) {
        if (IsDebug) alert("GlobalContext.prototype.SetMapOverlay  " + e.message);
        if (typeof (console) != 'undefined' && console) console.log("GlobalContext.prototype.SetMapOverlay "     + e.message);
    }
}

ko.applyBindings(lVM);
ko.applyBindings(footerViewModelLogin);
Run Code Online (Sandbox Code Playgroud)

如果没有要绑定的元素预定义事件,则淘汰失败.

Luf*_*ffy 0

是的,您可以使用 ko.applyBindingsToNode 函数将绑定重新应用到特定的 dom 元素。

检查这些例子

  1. http://jsfiddle.net/rniemeyer/gYk6f/ ---> 使用模板
  2. http://jsfiddle.net/rniemeyer/BnDh6/ ---> 带数据-值对