Dojoで簡単なフォームを書いています。これは、実際に何かを行うという意味で Dojo を「実際に」使用した最初の例です...基本的には:
1) ウィジェットが表示されるとき、onShow は、フォームが持つべき値をプリロードしようとします 2) ユーザーが送信すると、同じストアを使用して値が保存されます
ここでの「問題」は、1000% フェイルセーフなアプリケーションを作成しようとしているということです。たとえば、ストアが機能しない場合 (そのため設定を取得できない場合)、3 秒後、またはユーザーがリンクをクリックしたときにアプリケーションを再試行したいと考えています。そこで、私が今行っているのは、すべての「フェッチ」機能を関数 loadUp() に配置することです。問題がある場合は、再度 loadUp() を実行します (promise のエラー コールバックから)。また、フォームを無効にする必要があります (フォームをグレーにして入力を許可するなど)。
だから、質問:
1) これは正しいやり方ですか? 2) フォームをグレー表示にするにはどうしますか?
ここでワークスペースの設定について話していることに気づきました。したがって、実際に起こることは、「取得」がプロセス全体の最初に行われるということです。ただし、これをほとんどすべてのフォームに再利用します...
コードは次のとおりです。
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...
});
}
});