0

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... 

  }); 
} 
}); 
4

1 に答える 1

0

これが私がしたことです:

ステップ1:クラスにミックスインを追加しました:、ウィジェットを再表示するように_OverlayMixin設定しました。また、呼び出さ れたらすぐにonOverlayClick実行し、 (問題なく動作した場合)または ; (物事がうまくいかない場合)。this.set( 'overlayed', true ); this.set( 'overlayClickable', false );show()that.set( 'overlayed', false );that.set('overlayClickable', true )

ステップ2:実際に_OverlayMixinクラスを作成しました

コードは次のとおりです。クラスは本当にシンプルで、_OverlayMixin自己完結型です。かわいらしくする必要があります-今のところ、それはウィジェット上でただの灰色のものです。オーバーレイがクリック可能である場合、クラスが設定されているため、再試行するためのアイコンが表示される場合があります。とても道場っぽくて、とても読みやすいので、自分のやったことが好きです。また、この方法では、問題が発生した場合にユーザーがウィジェットを簡単に「再起動」できます。

これがコードです...私の単純なウィジェットから始めます:

var SettingsGeneral = declare('hotplate.bd.SettingsGeneral', [ ContentPane, _WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin, _OverlayMixin ], { 

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>', 


onOverlayClick: function(){ 
  this.onShow(); 
}, 

onShow: function(){ 

  var that = this; 

  // By default, this widget is overlayed 
  this.set( 'overlayed', true ); 
  this.set( 'overlayClickable', false ); 

  var workspaceSettings = stores('workspaceSettings', { workspaceIdCall: vars['hotDojoAppContainer']['workspaceId'] } ); 

  workspaceSettings.get('').then( 
    function(res){ 

      // OK things worked out: the overlay can go 
      that.set( 'overlayed', false ); 

      // 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){ 
      that.set('overlayClickable', true ); 
    } 
  ); 

}, 

ミックスイン:

define([ 
"dojo/_base/declare", 
"dojo/on", 
"dojo/dom", 
"dojo/dom-construct", 
"dojo/dom-attr", 
 "dojo/dom-class", 

], function( 
 declare 
, on 
, dom 
, domConstruct 
, domAttr 
, domClass 
){ 
return declare( null, { 

overlayed: false, 
overlayClickable: false, 

postCreate: function(){ 
  var that = this; 

  this.inherited(arguments); 

  // Create the overlay widget, place it as first in the widget 
  var overlay = domConstruct.create('div', { 
    className: 'overlay', 
    style: { 
      'position': 'absolute', 
      'top': 0, 
      'bottom': 0, 
      'left': 0, 
      'right': 0, 
      'z-index': 99, 
      'display': 'none', 
      'background': 'rgba(0,0,0,0.32)' 
    } 
  } ); 
  domConstruct.place(overlay, this.domNode, 'first'); 

  // Set the overlayNode attribute of the widget 
  this.overlayNode = overlay; 

  // Wire the click of the mouse to the object's "onOverlayClick" 
  on( this.overlayNode, 'click', function(e){ 
    if( that.overlayClickable && typeof( that.onOverlayClick ) === 'function' ) { 
      that.onOverlayClick(e); 
    } 
  }); 
}, 


// Set the widget as "overlayed" or 
_setOverlayedAttr: function( value ){ 
  this._set('overlayed', value); 
  if( value ){ 
    domAttr.set( this.overlayNode, { style: { display: 'block'} } ); 
  } else { 
    domAttr.set( this.overlayNode, { style: { display: 'none'} } ); 
  } 
}, 
_getOverlayedAttr: function(){ 
  return this.overlayVisible; 
}, 


// Set the widget as clickable or not 
_setOverlayClickableAttr: function(value){ 
  console.log("Set to clickableeeeeeeeeeeeeeeeeeeeeeeeeeeeee!"); 
  this._set('overlayClickable', !! value); 
  if( value ){ 
    console.log("1"); 
    domClass.add( this.overlayNode, 'overlayClickable' ); 
  } else { 
    console.log("2"); 
    domClass.remove( this.overlayNode, 'overlayClickable' ); 
  } 
}, 
_getOverlayClickableAttr: function(){ 
  return this.overlayClickableAttr; 
} 



});  
}); 
于 2012-12-07T05:48:53.463 に答える