1

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

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

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

4

2 回答 2

3

您可以使用全局变量container1VMcontainer2VM它。
例如调用

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

将使可见的容器 - 不可见。

JSFiddle 演示

代码:

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"));
于 2014-08-19T06:34:46.393 回答
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);
于 2014-08-19T13:10:52.613 回答