2016-06-22 4 views
2

私は次のウィンドウを定義した:私は、ウィンドウを表示する場合メンバーマップをツールバーの保存に使用できないのはなぜですか?

qx.Class.define('my.Window', { 
    ... 

    construct: function(caption, icon) { 
     this.base(arguments, caption, icon); 
     this.setLayout(new qx.ui.layout.VBox(10)); 

     this.add(new qx.ui.form.renderer.Single(this.getForm('some'))); 
     this.add(new qx.ui.form.renderer.Single(this.getForm('other'))); 

     this.add(this.getToolbar('test')); // This is shown 
     this.add(this.getToolbar('some')); // This is not shown 
     this.add(this.getToolbar('other')); // This is not shown 
    }, 

    members: { 
     _forms: {}, 
     _toolbars: {}, 

     getForm: function(id) { 
      if(this._forms[id]) return this._forms[id]; 

      if(id = 'some') this._forms[id] = new Form(); 
      else if(id == 'other') this._forms[id] = new OtherForm(); 

      return this._forms[id]; 
     }, 

     getToolbar: function(id) { 
      if(id == 'test') { 
      if(this._tb) return this._tb; 
      this._tb = new TestToolbar(); 
      return this._tb; 
      } 
      else if(id == 'some') this._toolbars[id] = new SomeToolbar(); 
      else if(id == 'other') this._toolbars[id] = new OtherToolbar(); 

      return this._toolbars[id];    
     } 
    } 
}); 

、フォーム(「一部」と「他の」)が示されています。 TestToolbarやListなどの他の項目も表示されます。しかし、他のツールバー( 'test'、 'some'、 'other')は表示されません。

_toolbarsをオブジェクトから配列に変更しようとしましたが、同じ動作があります。

+0

ノートで仕事をしてあなたの例を少し変更したバージョンだ...あなたは、コンストラクタでこのTODO持っているか、彼らはによって共有されますあなたのクラスのすべてのインスタンス... this._forms = {}; –

+0

これをプレイグラウンドhttps://demo.qooxdoo.org/current/playgroundに追加すると、他の人があなたのコードがどのように失敗するのかが分かりやすくなります。 –

+0

YeshuaはTobiに完全なplaygoundサンプルを作成してください。障害のある動作を複製することができれば、コードの何が間違っているかを簡単に知ることができます。しかし、あなたの質問への短い答えを与えるために、メンバーマップ、たとえツールバーのインスタンスでもインスタンスを保持することは可能です。 – level420

答えて

3

あなたの例では、getToolbar()メソッドは何を問わず常に新しいツールバーを作成します。これは、コンストラクタが新しいツールバーをウィンドウに追加する間に、その後getToolbar()を呼び出してそれにウィジェットを追加すると間違ったツールバーに追加している(つまり、ウィンドウに追加されていないツールバーに追加する)ことを意味します。

ここでは、オブジェクトとメンバープロパティを初期化しないでください遊び場

qx.Class.define('my.Window', { 
    extend: qx.ui.window.Window, 

    construct: function(caption, icon) { 
     this.base(arguments, caption, icon); 
     this.setLayout(new qx.ui.layout.VBox(10)); 

     this._toolbars = {}; 

     //commented out because 
     //this.add(new qx.ui.form.renderer.Single(this.getForm('some'))); 
     //this.add(new qx.ui.form.renderer.Single(this.getForm('other'))); 

     this.add(this.getToolbar('test')); // This is shown 
     this.add(this.getToolbar('some')); // This is not shown 
     this.add(this.getToolbar('other')); // This is not shown 

     this.getToolbar('test').add(new qx.ui.toolbar.Button("Test Button")); 
     this.getToolbar('some').add(new qx.ui.toolbar.Button("Some Button")); 
     this.getToolbar('other').add(new qx.ui.toolbar.Button("Other Button")); 
    }, 

    members: { 
     _forms: null, 
     _toolbars: null, 

     getForm: function(id) { 
      if(this._forms[id]) return this._forms[id]; 

      if(id == 'some') this._forms[id] = new Form(); 
      else if(id == 'other') this._forms[id] = new OtherForm(); 

      return this._forms[id]; 
     }, 

     getToolbar: function(id) { 
     if (!this._toolbars[id]) { 
      if(id == 'test') { 
      if(this._tb) return this._tb; 
      this._tb = new qx.ui.toolbar.ToolBar(); 
      return this._tb; 
      } 
      else if(id == 'some') this._toolbars[id] = new qx.ui.toolbar.ToolBar(); 
      else if(id == 'other') this._toolbars[id] = new qx.ui.toolbar.ToolBar(); 
     } 
     return this._toolbars[id];    
     } 
    } 
}); 
var win = new my.Window("First Window"); 
win.setWidth(300); 
win.setHeight(200); 
win.setShowMinimize(false); 

this.getRoot().add(win, {left:20, top:20}); 
win.open(); 
関連する問題