我正在用 Dojo 编写一个简单的表单。这是我在实际做某事方面对 Dojo 的第一次“真正”使用......基本上:
1)当显示小部件时,onShow,它会尝试预加载表单应具有的值 2)当用户提交时,使用相同的存储保存值
这里的“问题”是我正在尝试制作一个 1000% 的故障安全应用程序。例如,如果商店不工作(因此无法检索设置),我希望应用程序在 3 秒后重试 - 或者,当用户单击链接时。所以,我现在正在做的是将所有“获取”功能放入一个函数 loadUp() 中。如果有问题,我将再次运行 loadUp()(从 promise 中的错误回调)并且我还应该禁用表单——比如将其变为灰色,现在允许输入)。
所以,问题:
1)这是一种理智的做事方式吗?2)你会怎么做,关于灰色的表格?
我意识到我在这里谈论的是工作区设置。所以,真正会发生的是“get”将在整个过程的一开始就完成。但是,我将在几乎所有形式中重复使用它......
这是代码:
var SettingsGeneral = declare('hotplate.bd.SettingsGeneral', [ ContentPane, _WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin ], {
templateString: '' +
'<div>' +
' <form data-dojo-type="dijit.form.Form" data-dojo-attach-point="form" method="POST"> ' +
' <input id="${id}_WorkspaceLongName" data-dojo-type="dijit/form/ValidationTextBox" name="longName" data-dojo-props="" />' +
' <input id="${id}_WorkspaceTag" data-dojo-type="dijit/form/ValidationTextBox" name="tag" data-dojo-props="" />' +
' <input class="formSubmit" type="submit" data-dojo-attach-point="button" data-dojo-type="hotplate.hotDojoWidgets.BusyButton" label="Create!" />' +
' </form>' +
'</div>',
onShow: function(){
this.inherited(arguments);
// This returns a working store
var workspaceSettings = stores('workspaceSettings', { workspaceIdCall: vars['hotDojoAppContainer']['workspaceId'] } );
function loadUp(){
// The server will return the settings for this particular workspace
workspaceSettings.get('').then(
function(res){
// All good...
// Assign all of the received values to matching fields
// TODO: Turn this into a function
that.form._descendants.forEach(function( widget ) {
console.log(widget);
if( typeof( res.data[ widget.name ] ) !== 'undefined'){
widget.set('value', res.data[ widget.name] );
}
});
},
function(err){
// Error: try this again in 3 seconds, hopefully it will work. It would actually be better having a
// link to click for the user
console.log("Something went wrong. The form needs to be disabled (turned grey?) and the connection should be retried");
setTimeout(loadUp, 3000);
}
);
};
loadUp();
// Submit form
this.form.onSubmit = ds.defaultSubmit(this.form, this.button, function(){
console.log("PRESSED!");
that.button.cancel();
console.log(workspaceSettings);
// Use the same data store to save the settings...
});
}
});