多基因敲除绑定和共享它们 [英] multiple knockout bindings and sharing them

查看:132
本文介绍了多基因敲除绑定和共享它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个场景,我在我的主页上已经淘汰赛结合。

So I have a scenario where I am have knockout binding on my main page.

Index.cshtml:

Index.cshtml:

<div data-bind="foreach breadcrumbs">
<div>

<script>
 var IndexViewModel = function () {
    var self = this;
    self.breadcrumbs = ko.observableArray();
 };
 ko.applyBindings(IndexViewModel);
</script>

和局部视图,该Index.cshtml内部载荷。局部视图都有自己的淘汰赛绑定:

And a Partial View that loads inside the Index.cshtml. The partial view has its own knockout bindings:

<div id="someId">

</div>
<script>
  var PartialViewModel = function () {
        var self = this;
        self.someFunction = function(){
            // Access Breadcrumbs here
        };
  };
  ko.applyBindings(PartialViewModel, "someId");
</script>

我想从第二局部视图访问面包屑observableArray和添加项目给他们。我不是,如果这有可能,甚至肯定。请帮忙。我也使用sammy.js但为此它不是相关的。

I want to access the breadcrumbs observableArray from the second partial view and add items to them. I am not even sure if this possible. Please help. I am also using sammy.js but for this purpose its not that relevant.

推荐答案

我不喜欢有视图模型之间的依赖关系,所以我previously使用一个jQuery的pubsub插件,以便视图模型交谈对方在分离的方式。你可以看到在<一个是它的例子href=\"http://stackoverflow.com/questions/14555381/variable-dependency-with-knockoutjs/14559492#14559492\">this回答

I don't like having to have dependencies between view models, so I've previously used a jQuery pubsub plugin to allow view models to talk to each other in a decoupled manner. You can see an example of it in this answer

基本上,当你更新面包屑,请在致电新阵列发布和其他视图模型将订阅该事件。

Basically, when you update the breadcrumb, call publish with the new array, and the other view model would subscribe to that event.

这篇关于多基因敲除绑定和共享它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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