Dojo AMD:无法调用require中的函数 [英] Dojo AMD: Can't call a function inside a require

查看:105
本文介绍了Dojo AMD:无法调用require中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的是一个新手的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],
fu (DOMConstruct,domAttr,FilteringSelect,ItemFileReadStore,ComboBox,DateTextBox,Select,Memory){
//很多代码来创建表,由SEVERAL函数组成
函数createNewRow(tbl){.. 。}
函数function1(){...}
函数function2(){...}
函数function3(){...}
}

现在,添加空行按钮调用自己的函数addEmptyRow。

但是此功能我必须:

- 再次对每个dojo模块执行其他要求

- 我不能使用fillReportTable中的inside中的任何函数-功能。例如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],
函数(domConstruct,domAttr,FilteringSelect,ItemFileReadStore,ComboBox,DateTextBox,Select,Memory){
//很多代码创建表,由SEVERAL函数组成
}

这似乎与AMD非常复杂。

或者我在这里缺少一些明显的东西?

如果您将代码分解成很多小功能,那么您是否在EACH功能中再次执行require?或者你把所有的功能放在一个需要的完整列表?

如果你这样做第二种方式,你怎么能从窗口小部件事件调用这些功能?

解决方案

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



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){

函数fillReportTable(repId){
//很多代码创建表,由SEVERAL函数组成
函数createNewRow(tbl){...}
函数function1(){...}
函数function2(){...}
函数function3(){... }
}

function addEmptyRow(){
//很多代码来创建表,由SEVERAL函数组成
}

//返回一个公开两个函数的对象
return {
fillReportTable:fillReportTable,
addEmptyRow:addEmptyRow
}

});

使用您的模块:

 < HTML> 

< head>

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

< script data-dojo-config =async:truesrc =js / lib / dojo / dojo.js>< / script>

< / head>

...

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


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. :-(

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.

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

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 () {... } 
}

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
}

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/

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
    }

});

And use your module like this:

<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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆