我需要从另一个dojo模板小部件调用dojo模板小部件中的函数。问题是我需要将此函数称为静态函数,而不是创建模板的新实例 - 无论如何要执行此操作?在我的应用程序中,模板1是数据网格,而模板2是通过单击数据网格中的行打开的表单。我需要根据表单中采取的操作刷新数据网格
由于
Start Page:
<!DOCTYPE html>
<html >
<head>
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
<link rel="stylesheet" href="http://js.arcgis.com/3.10/js/dojo/dijit/themes/claro/claro.css">
<link rel="stylesheet" href="http://js.arcgis.com/3.10/js/esri/css/esri.css">
<script>
var dojoConfig = {
parseOnLoad:true,
async: true,
isDebug:true,
packages: [
{name: "Scripts", location: location.pathname.replace(/\/[^/]+$/, "") + "/Scripts"}
]
};
</script>
<script src="http://js.arcgis.com/3.10/"></script>
<script>
require(["Scripts/Mod1", "Scripts/Mod2"],
function (Mod1, Mod2) {
M1 = new Mod1();
M1.M1Method("call from main page");
// Mod1.M1Method("call from main page");//any way to make so this could work like static function?
});
</script>
</head>
<body class="claro">
<div>look here you</div>
</body>
</html>
模板1:
define(["Scripts/Mod2", "dojo/_base/declare", "dijit/_WidgetBase"],
function (Mod2, declare, _WidgetBase) {
return declare([_WidgetBase], {
M1Method: function (msg) {
alert(msg);
M2 = new Mod2();
M2.M2Method("call from Mod1"); //works great
// Mod2.M2Method("call from Mod1"); //any way to make so this could work like static function?
},
M1Method2: function (msg) {
alert(msg);
}
}
)
}
);
模板2:
define(["require", "dojo/_base/declare", "dijit/_WidgetBase"],
function (require, declare, _WidgetBase) {
return declare([_WidgetBase], {
M2Method: function (msg) {
alert(msg);
try {
require(["Scripts/Mod1"], function (Mod1) {
M1 = new Mod1();
M1.M1Method2("call from Mod2");
//Mod1.M1Method2("call from Mod2");//any way to make so this could work like static function?
});
} catch (dohObj) {
alert(dohObj.message);
}
}
}
)
}
);
答案 0 :(得分:0)
我不完全确定你的实际问题是什么,但如果你想创建静态方法&#34;,那么是的,它是可能的。只需在declare()
语句之后添加它们,如下所示:
var M1 = declare([_WidgetBase], {
/** Your code */
});
M1.M1Method = function(msg) {
/** Your static method */
};
return M1;
但是,请注意,如果您创建静态方法,则在创建实例时它不可用,例如以下方法将起作用:
M1.M1Method("test");
但这不再有效:
var m = new M1();
m.M1Method("test");
如果你想创建一个既可以作为原型的一部分而又可以作为&#34;静态方法&#34;的方法,那么你可以写下这样的东西:
/** An object containing your "static methods" */
var statics = {
static: function (msg) {
console.log("Static:" + msg);
}
};
/** Your actual prototype */
var M1 = declare([], lang.mixin({
nonStatic: function (msg) {
console.log("Non static:" + msg);
}
}, statics));
/** Mixin the statics */
lang.mixin(M1, statics);
return M1;
因此,我们在此处使用dojo/_base/lang::mixin()
两次混合静态对象,首先我们将其用于将其混合到原型中,因此您仍然可以在#39时使用该方法;有一个实例,然后我们将它混合到结果中。
我在JSFiddle上设置了一个基本示例:http://jsfiddle.net/g00glen00b/FE9Qz/