Dojo 加载器不加载回调中使用的小部件依赖项
Dojo loader not loading widget's dependencies used in callbacks?
我在 Dojo 的加载器上遇到了真正的问题。我发现在这里粘贴代码真的很难,因为这是一个涉及整个应用程序的问题。我创建了一个名为LoginForm
的小部件,它的开头是这样的:
define([
"dojo/_base/declare",
"app/globals",
"app/stores",
"dijit/form/Form",
"dijit/_WidgetBase",
"dijit/_TemplatedMixin",
"dijit/_WidgetsInTemplateMixin",
"dijit/layout/TabContainer",
"dijit/layout/StackContainer",
"dijit/layout/ContentPane",
"dijit/layout/BorderContainer",
"dijit/form/Button",
"app/widgets/ValidationPassword",
"app/widgets/ValidationEmail",
"app/widgets/AlertBar",
"app/widgets/StackFading",
"app/widgets/TabFading",
"dojo/_base/lang",
"app/widgets/BusyButton",
"dojo/_base/json",
"dojo/_base/fx",
"dojo/text!app/widgets/templates/LoginForm.html",
], function(
declare,
...
, json
, baseFx
, templateString
){
// Create the "login" pane, based on a normal ContentPane
return declare('app.LoginForm', [_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin ], {
widgetsInTemplate: true,
templateString: templateString,
这是一个使用模板的简单小部件,在模板中,一些子小部件,如此处所述。
现在。。。我正在尝试在 Ajax 回调中创建 Loginform 类型的对象。但是,加载程序让我失望了。
该应用程序的主屏幕如下所示:
define([
"dojo/_base/declare",
"dijit/_WidgetBase",
"dijit/_TemplatedMixin",
"dijit/_WidgetsInTemplateMixin",
"app/widgets/Dashboard",
"app/globals",
"dijit/layout/BorderContainer",
"dijit/layout/StackContainer",
"dijit/layout/TabContainer",
"dijit/layout/ContentPane",
"app/widgets/SearchPage",
], function(
declare
, _WidgetBase
...
, SearchPage
){
// Create the "login" pane, based on a normal ContentPane
return declare('AppMainScreen', [_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin ], {
。
第三个小部件,仪表板,只有一个按钮:
define([
"dojo/_base/declare",
"app/globals",
"app/defaultSubmit",
"app/stores",
"dijit/form/Form",
"dijit/_WidgetBase",
"dijit/_TemplatedMixin",
"dijit/_WidgetsInTemplateMixin",
"app/widgets/AlertBar",
"app/widgets/BusyButton",
"dojo/_base/json",
"dojo/domReady!",
], function(
declare
, g
, ds
, stores
, Form
, _WidgetBase
, _TemplatedMixin
, _WidgetsInTemplateMixin
, AlertBar
, BusyButton
, json
){
// Create the "login" pane, based on a normal ContentPane
return declare('app.Dashboard', [_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin ], {
widgetsInTemplate: true,
templateString: '' +
'<div>' +
' <div data-dojo-type="app.AlertBar" data-dojo-attach-point="alertBar"></div>' +
' <form data-dojo-type="dijit.form.Form" data-dojo-attach-point="form" method="POST"> ' +
' <input type="submit" data-dojo-attach-point="button" data-dojo-type="app.BusyButton" label="Create!" />' +
' </form>' +
'</div>',
然后,该表单的onSubmit链接到"app/defaultSubmit",
中的函数,该函数如下:
define([
'app/globals',
'dijit/registry',
'dojo/query',
'dojo/aspect',
'dojo/_base/lang',
'dojo/_base/json',
'dijit/Tooltip',
'dijit/Dialog',
'app/widgets/LoginForm',
],function(
g
, registry
, query
, aspect
, lang
, json
, Tooltip
, Dialog
, LoginForm
){
问题是LoginForm不起作用,因为LoginForm的构造函数不起作用:它说它不能实例化其中一个依赖类。
现在,如果我将其添加到应用程序的主屏幕的postCreate中:
var loginForm = new LoginForm({});
然后,在回调中,LoginForm() 神奇地工作 (!)。
我知道我没有发布所有代码,但是在AMD中如何处理依赖项方面,我需要了解什么魔术吗?如何确保在回调中解析模块...?
再见
默克。
这些评论并不适合评论部分。
我看不到您的代码中的缺陷,但这里有一些事情需要注意。
关于 AMD 加载程序的说明
不要对加载顺序做出假设。
require(["foo", "bar", function (foo, bar) {
// use foo and bar
});
由于加载是异步的,因此可以首先加载和调用foo
或bar
。唯一可以保证的是,当调用回调函数时,它们都将被加载。"foo"
解析的事情是其define
函数返回的任何内容;如果这是undefined
请确保函数返回一个值。
避免循环依赖。
//bar.js
define(["foo"], function (foo) {
return {};
});
//foo.js
define(["bar"], function (bar) {
return {};
});
行为是定义的(请参阅有关模块的文档),但可能难以管理。检查是否没有通过传递依赖项引入这些依赖项(A
-> B
-> C
-> A
.)
在尝试分析模板之前加载可传递模板依赖项。
define(["dojo/_base/declare", "dijit/_WidgetBase", "dojo/parser",
"dijit/form/Button" //need to load this even though it is
//not referenced in the function
],
function(declare, _WidgetBase, parser) {
// pretend this is some custom template format (e.g. DTL)
var template = "<div data-dojo-type='dijit.form.Button'></div>";
return declare("foo.Foo", [_WidgetBase, _TemplatedMixin], {
postCreate: function() {
this.domNode.innerHTML = template;
parser.parse(this.domNode);
}
});
});
在适当的情况下,将_WidgetsInTemplateMixin
与标准模板微件一起使用。
此外,请务必阅读 1.7 发行说明,了解陷阱和注意事项。
注意:此代码未经测试。
顺便说一句,数组上的尾随逗号将导致IE7中的错误。
- HTML5在画布中加载较小的图像并保存实际大小的图像
- YepNopeJS:在小JS文件完全加载之前触发回调
- 移动小程序而不重新加载
- Dojo 加载器不加载回调中使用的小部件依赖项
- 推特小部件并不总是加载
- KNOCKOUT.js从JavaScript库中加载小部件
- 如何点击加载谷歌翻译小部件
- 无法为动态加载的输入框加载日期选择器和小部件
- 加载一个大型 JavaScript 文件与多个整体大小较小的较小文件的性能
- 如何使用JavaScript或jQuery在页面加载时禁用浏览器最小化,最大化和关闭按钮
- 如何让 docpad 咕噜的骨架最小供应商 js 文件与实时重新加载
- 如何设置加载 JSON 图层的最小缩放比例
- 如何阻止 PHP 页面中的 JavaScript 代码段在屏幕分辨率较小的设备上加载
- 当我可以将它加载到iframe中时,为什么要担心制作一个纯javascript/jquery小部件
- 如何防止页面在表单提交时重新加载,并显示一个小文本说“已成功注册”
- Meteor:使用ReactiveVar创建一个加载小部件,用于订阅用户集合
- 同源策略与AJAX加载小部件
- 计数器代码加载小图像
- 加载小工具's加载消息未显示
- 加载内容后加载小部件