1

我正在使用 openTemplate 函数在#TreeDiv 中渲染一个动态树树视图。在为 dynatree 中的某个模板加载所有单元之前,我清空/清除 #TreeDiv 以删除现有树。这是第一次。

当我尝试在#TreeDiv 中为另一个模板加载其他单位时,我看不到我的单位数据,div 只是空的。不知何故,#TreeDiv 被破坏了,因为代码像第一次创建单元树一样正常运行。不同之处在于,在我第二次执行 .empty() 时,已经存在一棵树。

我错了什么?

function openTemplate(dlg, form) {
        $.ajax({
            url: $(form).attr('action'),
            type: 'POST',                     
            data: form.serialize(),
            success: function (response) {          
                if (response.success) {
                    dlg.dialog("close");                
                   $('#TreeDiv').empty();         
                   loadUnits(response.data);                    
                }
                else {
                    // Reload the dialog with the form to show model/validation errors 
                    dlg.html(response);
                }
            }
        });
    }

 function loadUnits(data) {       
        $('#TreeDiv').dynatree({
            onActivate: function (node) {
                getTestSteps(node.data.key);
            },            
            onLazyRead: function (node) {           
                node.appendAjax({
                    url: '@Url.Action("GetChildrenUnits","Unit")',
                    data: {"unitId": node.data.key, "templateId": 1 }
                });
            },
            children: data
        });
    }
4

1 回答 1

1

看起来在 loadUnits 调用之前对 empty() 的调用是不完整的。

您可以将 empty() 调用包装在 untilTimeout 中。或者,您可以封装在 try 或 if 语句中。IE,如果清空完成则调用 loadUnits

于 2012-06-04T17:45:20.387 回答