当子组件在knockout JS中加载完成时调用Parent Component函数

时间:2015-04-10 13:37:49

标签: javascript knockout.js

如何通知Parent Component子组件已完成加载(它已根据component lifecycle达到活动状态。

我想做这样的事情:

<parentComponent>
    <!-- Some other html code here -->
    <childComponent>
    </childComponent>
</parentComponent>

parentViewModel = function(params){
    var self = this;
    self.toBeCalledByChild = function() { //Code here }
}
childViewModel = function(params){
    //some code
    parentViewModel.toBeCalledByChild(); //this is to be called when child Component finishes loading
}

1 个答案:

答案 0 :(得分:0)

Knockout提供了几种跟踪变更,自定义绑定,订阅和计算可观察对象的方法我可能会使用订阅,如果你想避免直接引用observable,那么你可以使用解耦通信,订阅那里的变化&#39 ; sa beforeChange以及

Using KO’s Native PubSub for Decoupled Synchronization