gpt4 book ai didi

javascript - Dojo 加载程序未加载回调中使用的小部件的依赖项?

转载 作者:行者123 更新时间:2023-11-30 18:21:30 26 4
gpt4 key购买 nike

我在使用 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 ], {

...

第三个小部件 Dashboard 只有一个按钮:

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
});

foobar 可能首先加载和调用,因为加载是异步的。唯一可以保证的是,当回调函数被调用时,它们都会被加载。 "foo" 解析为它的 define 函数返回的内容;如果这是 undefined 确保函数正在返回一个值。

避免循环依赖。

//bar.js
define(["foo"], function (foo) {
return {};
});

//foo.js
define(["bar"], function (bar) {
return {};
});

行为已定义(参见 documentation on modules )但可能难以管理。检查您是否没有通过传递依赖项引入这些 (A -> B -> C -> A .)

在尝试之前加载传递模板依赖项 parse模板。

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);
}
});

});

_WidgetsInTemplateMixinstandard template widgets 一起使用在适当的地方。

此外,请务必阅读 1.7 release notes陷阱和注意事项。

注意:此代码未经测试。


顺便说一句,数组中的尾随逗号会在 IE7 中导致错误。

关于javascript - Dojo 加载程序未加载回调中使用的小部件的依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11956992/

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