是否有关于如何实现简单的登录页面/对话框任何的例子吗? 我一直在试图与道场样板做(检查我以前的问题: 对于道场MVC布局的实现 )。 到目前为止,我已经能够显示我的对话框。 但我希望能得到我的数据,并单击事件要具有例如一个警告框(与他的内容)。
我的看法:
<form action="Login" method="post" validate="true" id="loginForm">
<table width="258">
<tr>
<td><label>Login</label></td>
<td><input class="cell" type="text" trim="true" dojoType="dijit.form.TextBox" value="" name="login" id="userId"/></td>
</tr>
<tr>
<td><label>Password</label></td>
<td><input class="cell" type="password" trim="true" dojoType="dijit.form.TextBox" value="" name="password" id="password"/></td>
</tr>
<tr><td colspan="2"> </td></tr>
<tr>
<td colspan="2" align="center">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" valign="top"><button dojoType="dijit.form.Button" type="submit" id="LoginButton" onClick="connect">Ok</button></td>
<td align="left" valign="top"><button dojoType="dijit.form.Button" type="submit" onclick="document.Login.reset()" id="Cancel">Cancel</button></td>
<td><button dojoType="dijit.form.Button" type="submit" onclick="showDialog();" id="resetPassword"> Show Dialog </button></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
我的微件模块/班
define([
"dojo/_base/declare",
"dijit/_Widget",
"dijit/_TemplatedMixin",
"dijit/_WidgetsInTemplateMixin",
"dojo/text!app/views/Login/Login.html",
"dijit/Dialog",
"dijit/form/Button",
"dijit/form/TextBox"
], function(
declare,
_Widget,
_TemplatedMixin,
_WidgetsInTemplateMixin,
template,
Dialog
){
return declare([_Widget, _TemplatedMixin, _WidgetsInTemplateMixin, Dialog], {
templateString: template
});
});
现在,如果你检查HTML元素ID LoginButton应该在这种情况下,所谓的“连接”功能。 这应该显示(在一个警告框)的用户名和密码的电流输入。
我在哪里把我的功能? 的种类...
connect : function(){
alert("username :" + dom.byId("userId").value()
+ " Password: " + dom.byId("password").value());
}
编辑:新的代码
define([
"dojo/_base/declare",
"dijit/_Widget",
"dojo/dom",
"dijit/_TemplatedMixin",
"dijit/_WidgetsInTemplateMixin",
"dojo/text!app/views/Login/Login.html",
"dijit/Dialog",
"dijit/form/Button",
"dijit/form/TextBox"
], function(
declare,
dom,
_Widget,
_TemplatedMixin,
_WidgetsInTemplateMixin,
template,
Dialog
){
return declare("login", [_Widget, _TemplatedMixin, _WidgetsInTemplateMixin, Dialog], {
templateString: template,
postCreate: function() {
this.inherited(arguments);
// make reference to widget from the node attachment
this.submitButton = dijit.getEnclosingWidget(this.submitButtonNode);
// override its onSubmit
this.submitButton.onClick = function(){
alert("username :" + dom.byId("userId").value()
+ " Password: " + dom.byId("password").value());
};
},
// and a sample on how to implement widget-in-template stateful get/setter pattern
// e.g. if submitbutton label should change on some event, call as such:
// dijit.byId('loginForm').set("submitLabel", "Sendin login, please wait");
_setSubmitLabelAttr : function(value) {
return this.submitButton.set("label", value);
},
_getSubmitLabelAttr : function() {
return this.submitButton.get("label");
},
});
});
我main.js:
define([ 'dojo/has', 'require', 'dojo/_base/sniff'], function (has, require) {
var app = {};
if (has('host-browser')) {
require([ './views/Login', 'dojo/domReady!' ], function (Login) {
app.login = new Login().placeAt(document.body);
app.login.startup();
// And now…
app.login.show();
});
}
else {
console.log('Hello from the server!');
}
});
Answer 1:
我想补充我的解决方案: http://jsfiddle.net/phusick/tG8Sg/
这是一个有点由于的jsfiddle的约束扭曲,但主要原则是相同的,因为我在我的编码使用。
首先,登录过程(或任何其它形式的处理)被密封在对话内,并将其与应用程序的其余部分经由以为发射找齐通信dojo/Evented
。 这是如何工作的:
var loginDialog = new LoginDialog({ controller: loginController});
loginDialog.startup();
loginDialog.show();
loginDialog.on("cancel", function() {
console.log("Login cancelled.");
});
loginDialog.on("error", function() {
console.log("Login error.");
});
loginDialog.on("success", function() {
console.log("Login success.");
console.log(JSON.stringify(this.form.get("value")));
});
正如你可以在的jsfiddle看到,有两个模板对话框的模板和登录表单模板 ,我里面组装LoginDialog
构造。 原因是我通常会对也是一类包装dijit/form/Form
做一些神奇的超越标准dijit/form/Form
验证和数据序列化,但由于注册简单,它会是在的jsfiddle的单个文件乱七八糟我摆脱它。 从对话中分离形式的优点是可以同时使用相同的形式(即视图 )与所有形式特设代码别的地方,例如在ContentPane
。 的形式是有只是显示和收集数据向/从用户,它不应该直接与模型进行通信,即,web服务,存在一种用于该目的的控制器 :
var LoginController = declare(null, {
constructor: function(kwArgs) {
lang.mixin(this, kwArgs);
},
login: function(data) {
// simulate calling web service for authentication
var def = new Deferred();
setTimeout(lang.hitch(this, function() {
def.resolve(data.username == this.username && data.password == this.password);
}), 1000);
return def;
}
});
创建的实例LoginController
:
// provide username & password in the constructor
// since we do not have web service here to authenticate against
var loginController = new LoginController({username: "user", password: "user"});
并将它传递给LoginDialog
构造如上还没有看到:
var loginDialog = new LoginDialog({ controller: loginController});
最后LoginDialog
类:
var LoginDialog = declare([Dialog, Evented], {
READY: 0,
BUSY: 1,
title: "Login Dialog",
message: "",
busyLabel: "Working...",
// Binding property values to DOM nodes in templates
// see: http://www.enterprisedojo.com/2010/10/02/lessons-in-widgetry-binding-property-values-to-dom-nodes-in-templates/
attributeMap: lang.delegate(dijit._Widget.prototype.attributeMap, {
message: {
node: "messageNode",
type: "innerHTML"
}
}),
constructor: function(/*Object*/ kwArgs) {
lang.mixin(this, kwArgs);
var dialogTemplate = dom.byId("dialog-template").textContent;
var formTemplate = dom.byId("login-form-template").textContent;
var template = lang.replace(dialogTemplate, {
form: formTemplate
});
var contentWidget = new (declare(
[_Widget, _TemplatedMixin, _WidgetsInTemplateMixin],
{
templateString: template
}
));
contentWidget.startup();
var content = this.content = contentWidget;
this.form = content.form;
// shortcuts
this.submitButton = content.submitButton;
this.cancelButton = content.cancelButton;
this.messageNode = content.messageNode;
},
postCreate: function() {
this.inherited(arguments);
this.readyState= this.READY;
this.okLabel = this.submitButton.get("label");
this.connect(this.submitButton, "onClick", "onSubmit");
this.connect(this.cancelButton, "onClick", "onCancel");
this.watch("readyState", lang.hitch(this, "_onReadyStateChange"));
this.form.watch("state", lang.hitch(this, "_onValidStateChange"));
this._onValidStateChange();
},
onSubmit: function() {
this.set("readyState", this.BUSY);
this.set("message", "");
var data = this.form.get("value");
// ask the controller to login
var auth = this.controller.login(data);
Deferred.when(auth, lang.hitch(this, function(loginSuccess) {
if (loginSuccess === true) {
this.onLoginSuccess();
return;
}
this.onLoginError();
}));
},
onLoginSuccess: function() {
this.set("readyState", this.READY);
this.emit("success");
},
onLoginError: function() {
this.set("readyState", this.READY);
this.set("message", "Please try again.");
this.emit("error");
},
onCancel: function() {
this.emit("cancel");
},
_onValidStateChange: function() {
this.submitButton.set("disabled", !!this.form.get("state").length);
},
_onReadyStateChange: function() {
var isBusy = this.get("readyState") == this.BUSY;
this.submitButton.set("label", isBusy ? this.busyLabel : this.okLabel);
this.submitButton.set("disabled", isBusy);
}
});
请参阅在上述模板的jsfiddle 。 他们应该在单独的文件required
通过dojo/text!
一般。 我把它们放进<script type="text/template">
以适合的jsfiddle。
Answer 2:
Seing你模板窗口小部件,你想利用模板控件解析的。 来自http://dojotoolkit.org/documentation/tutorials/1.6/templated/下面介绍如何将事件附加到一个简单れ。
<div
data-dojo-attach-point="focusNode"
data-dojo-attach-event="ondijitclick:_onClick"
role="menuitem" tabindex="-1">
<span data-dojo-attach-point="containerNode"></span>
</div>
Seing你在模板有小工具,让您的孩子的小部件的引用,并通过这些引用设置其属性。
<td align="center" valign="top">
<button
id="LoginButton" type="submit"
dojoType="dijit.form.Button"
dojoAttachPoint="submitButtonNode">
Ok
</button>
</td>
创建因此它成为“兄弟”来的dijit / DojoX中/道场文件夹的文件夹,把它称为“应用”。 而继小部件的声明,放在一个文件名为app /空间/ MyWidget.js下;
define([
"dojo/_base/declare",
"dijit/_Widget",
"dijit/_TemplatedMixin",
"dijit/_WidgetsInTemplateMixin",
"dojo/text!app/views/Login/Login.html",
"dijit/Dialog",
"dijit/form/Button",
"dijit/form/TextBox"
], function(
declare,
_Widget,
_TemplatedMixin,
_WidgetsInTemplateMixin,
template,
Dialog
){
return declare("app.widget.MyWidget", [_Widget, _TemplatedMixin, _WidgetsInTemplateMixin, Dialog], {
templateString: template
postCreate: function() {
this.inherited(arguments);
// make reference to widget from the node attachment
this.submitButton = dijit.getEnclosingWidget(dojo.query(".dijitButton")[0], this.domNode);
// or simply dijit.byId('LoginButton');
// override its onSubmit
this.submitButton.onClick = function(){
alert("username :" + dom.byId("userId").value()
+ " Password: " + dom.byId("password").value());
};
},
// and a sample on how to implement widget-in-template stateful get/setter pattern
// e.g. if submitbutton label should change on some event, call as such:
// dijit.byId('loginForm').set("submitLabel", "Sendin login, please wait");
_setSubmitLabelAttr : function(value) {
return this.submitButton.set("label", value);
},
_getSubmitLabelAttr : function() {
return this.submitButton.get("label");
}
});
});
一旦文件已经到位,写你的HTML像这样:
<head>
<script src=...dojo...></script>
<script>
dojo.require([
"dojo/parser",
"app/widget/MyWidget",
"dojo/domReady!" ] , function(parser) {
parser.parse();
});
</script>
</head>
<body>
<form dojoType="app.widget.MyWidget"></div>
</body>
这将做到以下几点:
- 加载dojotoolkit(道场/的dojo.js)
- 创建一个名为“应用”命名空间具有子空间“窗口小部件”
- 呈现体(简单形式DOM节点)
- ondomready调用脚本,requireing
app.widget.MyWidget
- 进myWidget需要依赖
- parser.parse实例化
<form dojoType="mywidget">
Answer 3:
随着mschr的帮助。 我来到这个防空火炮特定的解决方案。
define([
"dojo/_base/declare",
"dojo/dom",
"dijit/_Widget",
"dijit/_TemplatedMixin",
"dijit/_WidgetsInTemplateMixin",
"dojo/text!app/views/Login/Login.html",
"dijit/Dialog",
"dijit/form/Button",
"dijit/form/TextBox"
], function(
declare,
dom,
_Widget,
_TemplatedMixin,
_WidgetsInTemplateMixin,
template,
Dialog
){
return declare([_Widget, _TemplatedMixin, _WidgetsInTemplateMixin, Dialog], {
templateString: template,
postCreate: function() {
this.inherited(arguments);
// make reference to widget from the node attachment
//this.submitButton = this.getChildren()[ButtonIndex];
// override its onSubmit
/*alert("username :" + dom.byId("userId").value()
+ " Password: " + dom.byId("password").value());*/
dojo.connect(this.loginButton, 'onclick', this._login);
},
// and a sample on how to implement widget-in-template stateful get/setter pattern
// e.g. if submitbutton label should change on some event, call as such:
// dijit.byId('loginForm').set("submitLabel", "Sendin login, please wait");
_login : function() {
var value = dom.byId("userId").value;
if(value)
alert("username: " + value);
},
// and a sample on how to implement widget-in-template stateful get/setter pattern
// e.g. if submitbutton label should change on some event, call as such:
// dijit.byId('loginForm').set("submitLabel", "Sendin login, please wait");
_setSubmitLabelAttr : function(value) {
return this.submitButton.set("label", value);
},
_getSubmitLabelAttr : function() {
return this.submitButton.get("label");
},
});
});
文章来源: Simple Login implementation for Dojo MVC