从另一个 dojo 模板小部件调用 dojo 模板小部件中的函数
call function in dojo template widget from another dojo template widget
我需要从另一个 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);
}
}
}
)
}
);
我不完全确定你的实际问题是什么,但如果你想创建 "static methods",那么是的,这是可能的。只需将它们添加到 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");
如果你想创建一个既可以作为原型的一部分又可以作为 "static method" 的方法,那么你可以这样写:
/** 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()
来混合静态对象两次,首先我们使用它来将它混合到原型中,这样当你有一个实例,然后我们也将其混合到结果中。
我在 JSFiddle 上设置了一个基本示例:http://jsfiddle.net/g00glen00b/FE9Qz/
我需要从另一个 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);
}
}
}
)
}
);
我不完全确定你的实际问题是什么,但如果你想创建 "static methods",那么是的,这是可能的。只需将它们添加到 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");
如果你想创建一个既可以作为原型的一部分又可以作为 "static method" 的方法,那么你可以这样写:
/** 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()
来混合静态对象两次,首先我们使用它来将它混合到原型中,这样当你有一个实例,然后我们也将其混合到结果中。
我在 JSFiddle 上设置了一个基本示例:http://jsfiddle.net/g00glen00b/FE9Qz/