如何在knockout js中交换两个视图模型之间的值

时间:2014-08-19 06:03:19

标签: javascript knockout.js knockout-postbox

您好我有两个视图模型,我想在它们之间传输一个值。 这里是js小提琴

`http://jsfiddle.net/sohimohit/e84u8L85/`

我希望当用户点击显示另一个div按钮时,相应的项目名称应显示在另一个div上。意味着我想将一个viewmodel值显示到另一个。 其次,当用户点击显示另一个div按钮时,出现另一个div我想要一个取消选项,以便用户可以返回到firstdiv。我怎样才能做到这一点。

2 个答案:

答案 0 :(得分:3)

您可以使用全局变量container1VMcontainer2VM 例如。致电

 container1VM.isVisible(!container1VM.isVisible());
 container2VM.isVisible(!container2VM.isVisible());  

将使可见容器 - 不可见。

JSFiddle DEMO

<强>代码:

HTML:

   <div id="container1">


       <div data-bind="visible: !isVisible()">
        <ul >
            <li >Container1 item</li>
            <!-- ko foreach: myItems -->
            <li>Item <span data-bind="text: $data"></span></li>
            <button data-bind="click:$root.showDiv">show another div</button>
            <!-- /ko -->
        </ul>

       </div>
    </div>
     <div id="container2" data-bind="visible:isVisible">
        <ul>
            <li >Container2 item</li>
            <!-- ko foreach: myItems -->
                <li>Item <span data-bind="text: $data"></span></li>
            <!-- /ko -->
        </ul>
         <button data-bind="click:$root.cancel">cancel</button> 
    </div>   

Javascript:

function Container1ViewModel()
{
    var self = this;
    self.isVisible = ko.observable(false);
    self.showDiv = changeVisibility;
    self.myItems = ko.observableArray();
    self.myItems.push("ABC");
    self.myItems.push("CDE");

} 
function Container2ViewModel() {
    var self = this;
    self.isVisible = ko.observable();
    self.myItems = ko.observableArray();
    self.myItems.push("XYZ");
    self.myItems.push("PQR");
    self.cancel = changeVisibility;
}

function changeVisibility()
{
   container1VM.isVisible(!container1VM.isVisible());
   container2VM.isVisible(!container2VM.isVisible());
}

var container1VM = new Container1ViewModel();;
var container2VM = new Container2ViewModel();;

ko.applyBindings(container1VM, document.getElementById("container1"));

ko.applyBindings(container2VM, document.getElementById("container2"));

答案 1 :(得分:3)

还可以考虑使用优秀的Ryan Niemeyer的Knockout Postbox: https://github.com/rniemeyer/knockout-postbox

您可以使用syncWith更新可观察的控制可见性。这样,两个视图模型都不需要彼此了解,并且您不会硬编码对视图模型的引用。

syncWith - syncWith(topic,[initializeWithLatestValue],[skipInitialPublish],[equalityComparer])

syncWith函数告诉observable同时订阅和发布主题。这允许两个不同视图模型中的可观察者在不直接了解其对应物的情况下保持彼此同步。

//subscribe to and publish on a topic
this.value = ko.observable(value).syncWith("mytopic");

//subscribe to and publish on a topic and use the last published value to initialize the     observable
this.value = ko.observable().syncWith("mytopic", true);

//subscribe to and publish on a topic, but do not publish out the observable's value initially
this.value = ko.observable(value).syncWith("mytopic", false, true);

//subscribe to and publish on a topic, but only publish when the comparer function returns false
var comparer = function(newValue, oldValue) {
    return newValue < oldValue;
};

this.value = ko.observable(value).syncWith("mytopic", false, false, comparer);