简体   繁体   English

Dojo需要()和AMD(1.7)

[英]Dojo require() and AMD (1.7)

I'm having a heckuva time transitioning to Dojo and the new AMD structure, and I'm really hoping someone can shed some light on the whole concept. 我有一个heckuva时间过渡到Dojo和新的AMD结构,我真的希望有人可以对整个概念有所了解。 I've been living on Google for the last few weeks trying to find information on not the usage, but the structure and design pattern trends in using this. 过去几周我一直在Google上生活,试图找到关于不使用的信息,但是使用它的结构和设计模式趋势。

I find it strange that for a relatively complex javascript application, such as for a main page where Dijits need to be created and styled, DOM elements created, etc, that I need to require, and therefore use, a TON of different modules that were otherwise available in the dojo namespace before the AMD system (or, at least, not assigned to 23 different vars). 我发现奇怪的是,对于一个相对复杂的javascript应用程序,例如需要创建和设置Dijits的主页面,创建的DOM元素等,我需要,因此使用,不同模块的TON否则在AMD系统之前的dojo命名空间中可用(或者,至少不分配给23个不同的vars)。

Example: 例:

require(['dijit/form/ValidationTextBox', 'dijit/form/SimpleTextarea', 'dijit/form/CheckBox', 'dijit/Dialog', 'dijit/form/Form'])
require(['dojo/ready', 'dojo/parser', 'dojo/dom-style', 'dijit/registry', 'dojo/dom', 'dojo/_base/connect', 'dojo/dom-construct'], 
function(ready, parser, style, registry, dom, event, construct){
    //...etc
}

That's only a few of the modules for one of the pages I'm working on. 这只是我正在处理的其中一个页面的一些模块。 Surely there's a better, non-breaking-in-future-releases way of accessing these methods, etc. I mean, do I really have to import an entirely new module to use byId() ? 当然,有一种更好的,非byId()的未来发布方式来访问这些方法,等等。我的意思是,我真的必须导入一个全新的模块才能使用byId()吗? And yet another to connect events? 而另一个连接事件? On top of that, all the clutter being created by having to assign a variable name in the functions argument list to cling to just seems like such a backstep. 最重要的是,通过必须在函数参数列表中分配变量名来创建的所有混乱只是看起来像这样的后退。

I thought maybe you would require() the module only when needed, such as the query module, but if I need it more than once, then chances are the variable it's assigned to is out of scope, and I'd need to put it in a domReady! 我想也许你会在require()模块,比如query模块,但是如果我不止一次需要它,那么它被赋予的变量很可能超出了范围,我需要把它放到在domReady! or ready call. ready电话。 reaalllly....??! reaalllly .... ??!

Which is why I can only assume it's my lack of understanding for dojo. 这就是为什么我只能假设我对道场缺乏了解。

I really have looked and searched and bought books (albeit, a pre-AMD one), but this library is really giving me a run for my money. 我真的已经查看并搜索并购买了书籍(虽然是AMD之前的一本书),但这个图书馆真的让我为我的钱买单。 I appreciate light anyone can shed on this. 我很欣赏任何人都能看到的光。

Edit for Example 编辑示例

require(['dijit/form/ValidationTextBox'])
require(['dojo/ready', 'dojo/parser', 'dojo/dom-style', 'dijit/registry', 'dojo/dom', 'dojo/_base/connect', 'dojo/dom-construct'], function(ready, parser, style, registry, dom, event, construct){
    /* perform some tasks */
    var _name = new dijit.form.ValidationTextBox({
        propercase : true,
        tooltipPosition : ['above', 'after']
    }, 'name')

    /*
    Let's say I want to use the query module in some places, i.e. here.
    */
    require(['dojo/query', 'dojo/dom-attr'], function(query, attr){
        query('#list li').forEach(function(li){
            // do something with these
        })
    })
}

Based off of this format, which is used with many examples both from the dojo toolkit folks as well as third party sites, it would be, IMHO, absolutely ridiculous to load all the required modules as the first function(ready, parser, style, registy... would get longer and longer, and create problems with naming collisions, etc. 基于这种格式,与dojo工具包人员以及第三方网站的许多示例一起使用,恕我直言,加载所有必需的模块作为第一个function(ready, parser, style, registy...非常荒谬function(ready, parser, style, registy...会越来越长,并且会在命名冲突等方面产生问题。

Firing up and require() ing all the modules I would need during the life of the script just seems silly to me. 在脚本的生命周期中,我需要点击并require()所有模块对我来说似乎很愚蠢。 That being said, I'd have to look at some of the "package manager" scripts. 话虽这么说,我不得不看一些“包管理器”脚本。 But for this example, if I wanted to use the query module in select places, I would either have to load it up with the rest in the main require() statement. 但是对于这个例子,如果我想在选择的地方使用查询模块,我要么必须在主要的require()语句中加载其余部分。 I understand why to an extent, but what's so bad with generic dot-syntax namespaces? 我理解为什么在一定程度上,但通用点语法命名空间有什么用呢? dojo.whatever? dojo.whatever? dijit.findIt()? dijit.findIt()? Why load module, reference in a unique name, pass through closure, blah blah? 为什么加载模块,以唯一名称引用,通过闭包,等等等等?

I wish this were an easier question to ask, but I hope that makes sense. 我希望这是一个更容易提出的问题,但我希望这是有道理的。

Exasperation 恼怒

Call me a newb, but this is really.. really.. driving me mad. 叫我一个新手,但这真的......真的......让我发疯。 I'm no noob when it comes to Javascript (apparently not) but wow. 当谈到Javascript(显然不是)时,我不是菜鸟,但哇。 I cannot figure this 我无法想象这一点 out! 出来了!

Here's what I'm gathering. 这就是我正在收集的内容。 In adder.js: 在adder.js中:

define('adder', function(require, exports){
    exports.addTen = function(x){
        return x + 10
    }
})

In some master page or whatever: 在某些母版页或其他内容中:

require(['./js/cg/adder.js'])

...which doesn't follow the neat require(['cg/adder']) format but whatever. ......不遵循整洁的require(['cg/adder'])格式,但无论如何。 Not important right now. 现在不重要。

Then, the use of adder should be: 然后, adder的使用应该是:

console.log(adder.addTen(100)) // 110

The closest I got was console.log(adder) returning 3 . 我得到的最接近的是console.log(adder)返回3 Yep. 是的。 3 . 3 Otherwise, it's adder is not defined . 否则,它的adder is not defined

Why does this have to be so difficult? 为什么这么难? I'm using my noob card on this, cause I really have no idea why this isn't coming together. 我正在使用我的noob卡,因为我真的不知道为什么这不会聚在一起。

Thanks guys. 多谢你们。

The dependency array format: 依赖数组格式:

define(["a", "b", "c"], function (a, b, c) {
});

can indeed be annoying and error-prone. 确实很烦人且容易出错。 Matching up the array entries to function parameters is a real pain. 将数组条目与函数参数匹配是一个真正的痛苦。

I prefer the require format ( "Simplified CommonJS Wrapper" ): 我更喜欢require格式( “Simplified CommonJS Wrapper” ):

define(function (require) {
    var a = require("a");
    var b = require("b");
    var c = require("c");
});

This keeps your lines short and allows you to rearrange/delete/add lines without having to remember to change things in two places. 这样可以缩短线条,并允许您重新排列/删除/添加线条,而不必记住在两个地方更改内容。

The latter format will not work on PS3 and older Opera mobile browsers, but hopefully you don't care. 后一种格式不适用于PS3和旧的Opera移动浏览器,但希望你不在乎。


As for why doing this instead of manually namespacing objects, @peller's answer gives a good overview of why modularity is a good thing, and my answer to a similar question talks about why AMD and module systems as a way of achieving modularity are a good thing. 至于为什么这样做而不是手动命名空间对象,@ peller的回答很好地概述了为什么模块化是一件好事, 我对类似问题的回答谈到为什么AMD和模块系统作为一种实现模块化的方式是一件好事。

The only thing I would add to @peller's answer is to expand on "paying attention to dependencies actually makes for much better code." 我唯一要补充的是@peller的答案是扩展“关注依赖实际上会产生更好的代码。” If your module requires too many other modules, that's a bad sign! 如果你的模块需要太多其他模块,这是一个不好的迹象! We have a loose rule in our 72K LOC JavaScript codebase that a module should be ~100 lines long and require between zero and four dependencies. 我们在72K LOC JavaScript代码库中有一个松散的规则,模块应该是~100行长,并且需要0到4个依赖项。 It's served us well. 它很好地服务于我们。

requirejs.org gives a pretty good overview of what AMD is and why you'd want to use it. requirejs.org非常好地概述了AMD是什么以及为什么要使用它。 Yes, Dojo is moving towards smaller modules which you would reference individually. 是的,Dojo正在向更小的模块发展,您可以单独参考这些模块。 The result is that you load less code, and your references to it are explicit. 结果是您加载的代码更少,并且您对它的引用是显式的。 Paying attention to dependencies actually makes for much better code, I think. 我认为,关注依赖关系实际上会产生更好的代码。 AMD enables optimizations, and once the migration is complete, you don't have to load everything into globals anymore. AMD支持优化,一旦迁移完成,您就不必再将所有内容加载到全局变量中。 No more collisions! 没有更多的碰撞! The require() block wraps the code which uses various modules. require()块包装使用各种模块的代码。 domReady! domready中! relates to the loading of the DOM and has nothing to do with variables being in scope. 与DOM的加载有关,与范围内的变量无关。

Anyway, this is deviating from the Q&A format of SO. 无论如何,这偏离了SO的Q&A格式。 You might want to ask specific questions. 您可能想询问具体问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM