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

时间:2013-06-05 08:20:23

标签: javascript enyo

我在Enyo中使用窗口视图,它基本上从数据库中提取数据,并基于no。获取的项目,多个按钮是动态创建的。在单击任何按钮时,将再次调用数据库以获取其他项目集。取出的物品需要动态地添加到< ul>。项目作为按钮。这是由代码完成的 -

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

下找到

任何帮助将不胜感激。感谢!!!

2 个答案:

答案 0 :(得分:2)

我不确定这是 的方法,但是当我动态创建子组件时,我将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();
}

答案 1 :(得分:1)

在异步调用之后运行testPOSView以获取db结果时,很可能会失去指向此指针。

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

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

顺便说一句,如果你正确地绑定你的功能,你可能会消除我所有的这种愚蠢行为。