简体   繁体   English

Durandal:在一个孩子中创建功能并从另一个孩子中消费

[英]Durandal: Create function in one child and Consume from another child

I have a Knockout application developed using Durandal.JS . 我有一个使用Durandal.JS开发的Knockout应用程序。 Here is the structure of my project: 这是我的项目的结构:

- Parent
    - Index.js
    - Index.html
- Child1
    - Index.js
    - Index.html
- Child2
    - Index.js
    - Index.html

I have a function UpdateData() declared in Child2/Index.js and I want to consume that function from Child1/Index.js 我有一个函数UpdateData()在声明Child2/Index.js ,我想消费从功能Child1/Index.js

Reason of declaring this function in Child2 is, I have other variables and controls declared in Child2. Child2中声明此功能的Child2是,我还有在Child2中声明的其他变量和控件。 And by calling UpdateData() method, I want to update the value of all variables declared in Child2. 通过调用UpdateData()方法,我想更新在Child2中声明的所有变量的值。

I don't know how to handle this situation. 我不知道该如何处理。

Durandal by default uses require to load modules. 默认情况下,Durandal使用require来加载模块。 You could try to require the child2 module in your child1 module. 您可以尝试在child1模块中要求使用child2模块。

//child1 module
define(function (require) {

//require child2 into the module
var child2 = require('Child2/Index');

//use the method
child2.UpdateData();
});

For this to work, you must return the UpdateData() method from Child2 . 为此,您必须从Child2返回UpdateData()方法。

//child2 module
define(function(){
return {
UpdateData:function(){
  //your method
}
}});

For more information, you could go through the docs on how to create a module that can be reused by other modules. 有关更多信息,您可以阅读有关如何创建可被其他模块重用的模块的文档。 http://durandaljs.com/documentation/Creating-A-Module.html http://durandaljs.com/documentation/Creating-A-Module.html

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

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