为什么成员映射不能用来存储工具栏?
Why can't a member map be used to store toolbars?
我定义了以下内容window:
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];
}
}
});
当我显示 window 时,会显示表格('some' 和 'other')。还显示其他项目,例如 TestToolbar 和 List。但是,其他工具栏('test'、'some' 和 'other')未显示..
我试图将 _toolbars
从对象更改为数组,但我得到了相同的行为。
在您的示例中,getToolbar() 方法无论如何都会创建一个新的工具栏;这意味着虽然构造函数将向 window 添加一个新工具栏,但如果您随后调用 getToolbar() 并向其添加小部件,则您将添加到错误的工具栏(即添加到尚未添加到的工具栏window).
这里是你的例子的一个稍微修改过的版本,它在 playground 中确实有效
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();
我定义了以下内容window:
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];
}
}
});
当我显示 window 时,会显示表格('some' 和 'other')。还显示其他项目,例如 TestToolbar 和 List。但是,其他工具栏('test'、'some' 和 'other')未显示..
我试图将 _toolbars
从对象更改为数组,但我得到了相同的行为。
在您的示例中,getToolbar() 方法无论如何都会创建一个新的工具栏;这意味着虽然构造函数将向 window 添加一个新工具栏,但如果您随后调用 getToolbar() 并向其添加小部件,则您将添加到错误的工具栏(即添加到尚未添加到的工具栏window).
这里是你的例子的一个稍微修改过的版本,它在 playground 中确实有效
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();