无法在Enyo中动态添加组件(按钮)

Unable to add components(buttons) dyanamically in Enyo

本文关键字:组件 按钮 添加 动态 Enyo      更新时间:2023-09-26

我使用的是Enyo中的窗口视图,它基本上从数据库中获取数据,并基于no。获取项,动态创建多个按钮。在单击任何按钮时,将对数据库进行另一次调用以获取其他项集。获取的项需要动态地添加到

    项作为按钮。这是由代码- 完成的
    testPOSView : function(inSender, inEvent) {
            var data = inEvent.data;
            console.log(data.tables);
            enyo.forEach(data.tables, function(table) {
                console.log(table);
                this.$.sectiontablebar.createComponent({
                    kind : 'OB.OBPOSPointOfSale.UI.TablesButton',
                    button :  {
                        kind : 'OB.UI.Section',
                        content: table.tableName,
                        id: table.tableId
                    }
                });
            }, this);
        }
    

    但是当我点击按钮时,我从DB得到结果,但它们没有添加到sectiontablebar组件。

    文件的完整代码可在@ https://gist.github.com/sangramanand/ad665db9cd438001254a

    任何帮助都会很感激。谢谢! !

我不确定这是方法,但是当我动态创建子组件时,我将this.render()添加到函数的末尾。这将呈现组件,从而显示动态添加的内容。

如果我重写你的代码,我会这样做:

testPOSView : function(inSender, inEvent) {
    var data = inEvent.data;
    enyo.forEach(data.tables, function(table) {
        // create the sub-component in "this"
        this.createComponent({
            // and assign the container
            container: this.$.sectiontablebar,
            kind : 'OB.OBPOSPointOfSale.UI.TablesButton',
            button :  {
                kind : 'OB.UI.Section',
                content: table.tableName,
                id: table.tableId
            }
        });
    }, this);
    this.render();
}

当testPOSView在异步调用后运行以获得db结果时,更有可能丢失指向此的指针。

在你的anythingTap函数(见要点,读者)中,你可以尝试绑定你要发送到OB.DS.Process的函数:

this.bindSafely(function(data) {me.doTestPOSView();}))

顺便说一下,如果你正确地绑定你的函数,你可能会消除所有这些愚蠢的事情。