Dojo AMD:无法在 require 中调用函数 [英] Dojo AMD: Can't call a function inside a require
问题描述
我确实是 dojo 的新手,但是当我开始使用 1.7.2 版 dojo 开发新应用程序时,我还想使用新的 AMD 函数语法.不幸的是,我似乎不明白.:-(
I am really a newbie to dojo but as i started developing a new application with dojo version 1.7.2 i also wanted to use the new AMD syntax for functions. Unfortunately i don't seem to get it. :-(
最让我烦恼的是我不能简单地调用require"块内的任何函数.例如,我有一个页面,在打开时会创建一个动态表,每行中有几个小部件.然后我有一个按钮,每次按下都会添加一个空行.
What annoys me most is that i can't simply call any function which is inside of a "require"-block. For example i have a page which on opening creates a dynamic table with several widgets in each row. Then i have a button which adds one empty row each time pressed.
没有 AMD 语法会很容易:
- 将我所有的dojo.require()"放在 HEAD
- 然后创建一堆我自己的函数来创建表格和小部件
- 添加行函数可以轻松访问我之前的函数填充的任何全局变量
Without AMD syntax it would be easy:
- put all my "dojo.require()" in the HEAD
- and then create a bunch of my own functions for creating the table and widgets
- the add row function could easily access any global-variables my previous function filled
但是对于 AMD,它是这样的:
But with AMD its like this:
初始函数创建表格和小部件:
Initial function creates the table and widgets:
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"功能
Now the "Add Empty Row" button calls its own function "addEmptyRow".
But in this function I have to:
- do an other require for each dojo-module again
- I CAN'T use any of the functions which are "inside" of the "fillReportTable"-function. For example the "createNewRow"-function
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,如果您将代码分成许多小函数,您是否会在 EACH 函数中重新执行require"?或者您是否将所有功能放在一个带有完整列表的要求"中?
如果您采用第二种方式,您如何从小部件事件中调用这些函数?
This all seems to be so much complicated with AMD.
Or am i missing something obvious here ?
With AMD if you separate your code into a lot of small functions, do you do the "require" inside EACH function all over again ? Or do you put all the functions inside one "require" with the full list ?
If you do it the second way, how can you call these functions from widget events?
推荐答案
最简单的方法是定义自己的模块.先看看这个教程:
The easiest way would be to define your own module. Take a look at this tutorial first:
http://dojotoolkit.org/documentation/tutorials/1.7/modules/
现在定义你自己的模块,例如./js/mymodules/mymodule.js"(相对于 HTML 页面):
Now define your own module, e.g. "./js/mymodules/mymodule.js" (relative to HTML page):
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>
这篇关于Dojo AMD:无法在 require 中调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!