简体   繁体   English

使用Knockout.js在jQuery Mobile中将模型对象与多个页面绑定

[英]Bind a Model Object with multiple pages in jQuery Mobile with Knockout.js

I have knockout js Model, I want to bind that to two pages in jQuery mobile, the first page only contains read only attributes of the model and other page contains elements with which the user can update fields. 我有淘汰赛js模型,我想将其绑定到jQuery mobile中的两个页面,第一页仅包含模型的只读属性,其他页面包含用户可以用来更新字段的元素。

function ModelViewObject(){
    this.id = "1001";
    this.firstName = ko.observable("First Name"); 
    this.lastName = ko.observable("Last Name");
}

//This page only contains read only values
<div data-role="page" id="pageOne">
    <h1>Id : <span data-bind="text: id()</h1>
</div>

//This page can edit
<div data-role="page" id="pageTwo">
   <p>First name: <input data-bind="value: firstName" /></p>
    <p>Last name: <input data-bind="value: lastName" /></p>
</div>

I could bind it first page or second page at a time.I want to bind the model with two pages, I would really appreciate if some one can help me to solve this. 我可以一次绑定第一页或第二页。我想将模型与两页绑定,如果有人可以帮助我解决这个问题,我将非常感激。

Thank you. 谢谢。

You can bind this way 你可以这样绑定

ko.applyBindings(new ModelViewObject(), document.getElementById("pageOne"));
ko.applyBindings(new ModelViewObject(), document.getElementById("pageTwo"));

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

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