gpt4 book ai didi

道场 AMD : Can't call a function inside a require

转载 作者:行者123 更新时间:2023-12-03 23:34:17 25 4
gpt4 key购买 nike

我真的是 dojo 的新手,但是当我开始使用 dojo 版本 1.7.2 开发新应用程序时,我也想使用新的 AMD 函数语法。不幸的是,我似乎没有得到它。 :-(

最让我烦恼的是,我不能简单地调用“require” block 内的任何函数。
例如,我有一个页面,在打开时会创建一个动态表,每行中有几个小部件。
然后我有一个按钮,每次按下时都会添加一个空行。

如果没有 AMD 语法,它会很容易:
- 把我所有的“dojo.require()”放在 HEAD
- 然后创建一堆我自己的函数来创建表格和小部件
- 添加行函数可以轻松访问我之前的函数填充的任何全局变量

但是对于 AMD,它是这样的:

初始函数创建表和小部件:

function fillReportTable(repId) {
require(["dojo/dom-construct", "dojo/dom-attr", "dijit/form/FilteringSelect",
"dojo/data/ItemFileReadStore", "dijit/form/ComboBox", "dijit/form/DateTextBox", "dijit/form/Select", "dojo/store/Memory"],
function (domConstruct, domAttr, FilteringSelect, ItemFileReadStore, ComboBox, DateTextBox, Select, Memory) {
// a lot of code to create the table, consisting of SEVERAL functions
function createNewRow(tbl) { ...}
function function1 () {... }
function function2 () {... }
function function3 () {... }
}

现在“添加空行”按钮调用它自己的函数“addEmptyRow”。
但在这个功能中,我必须:
- 再次为每个 dojo 模块执行另一个要求
- 我不能使用“fillReportTable”函数“内部”的任何函数。例如“createNewRow”函数
 function addEmptyRow() {
require(["dojo/dom-construct", "dojo/dom-attr", "dijit/form/FilteringSelect",
"dojo/data/ItemFileReadStore", "dijit/form/ComboBox", "dijit/form/DateTextBox", "dijit/form/Select", "dojo/store/Memory"],
function (domConstruct, domAttr, FilteringSelect, ItemFileReadStore, ComboBox, DateTextBox, Select, Memory) {
// a lot of code to create the table, consisting of SEVERAL functions
}

对于 AMD,这一切似乎都非常复杂。
还是我在这里遗漏了一些明显的东西?
对于 AMD,如果您将代码分成许多小函数,您是否会重新执行每个函数中的“要求”?或者您是否将所有功能放在一个带有完整列表的“要求”中?
如果你用第二种方法,你怎么能从小部件事件中调用这些函数呢?

最佳答案

最简单的方法是定义自己的模块。先看看这个教程:

http://dojotoolkit.org/documentation/tutorials/1.7/modules/

现在定义你自己的模块,例如“./js/mymodules/mymodule.js”(相对于 HTML 页面):

define([
"dojo/dom-construct",
"dojo/dom-attr",
"dijit/form/FilteringSelect",
"dojo/data/ItemFileReadStore",
"dijit/form/ComboBox",
"dijit/form/DateTextBox",
"dijit/form/Select",
"dojo/store/Memory"
], function (domConstruct, domAttr, FilteringSelect, ItemFileReadStore, ComboBox, DateTextBox, Select, Memory) {

function fillReportTable(repId) {
// a lot of code to create the table, consisting of SEVERAL functions
function createNewRow(tbl) { ...}
function function1 () {... }
function function2 () {... }
function function3 () {... }
}

function addEmptyRow() {
// a lot of code to create the table, consisting of SEVERAL functions
}

// Return an object that exposes two functions
return {
fillReportTable: fillReportTable,
addEmptyRow: addEmptyRow
}

});

并像这样使用您的模块:
<html>

<head>

<script>
var dojoConfig = {
baseUrl: "./js/",
packages: [
{ name: "dojo", location: "lib/dojo" },
{ name: "dijit", location: "lib/dijit" },
{ name: "dojox", location: "lib/dojox" }
]
};
</script>

<script data-dojo-config="async: true" src="js/lib/dojo/dojo.js"></script>

</head>

...

<script>
require([
"mymodules/mymodule"
], function (mymodule) {
mymodule.fillReportTable(...);
mymodule.addEmptyRow(...);
});
</script>

关于道场 AMD : Can't call a function inside a require,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9626821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com