1

我正在编写一个家谱应用程序,但无法动态地将孩子与父母联系起来。我不断收到父母未定义的错误。由于使用相同的函数来创建父级,我不知道如何识别父级,而不是在再次调用该函数时通过变量名传递它。

非常感谢任何可以为我指明正确方向的人。我显然不是很精通javascript。

更新:我能够通过将每个函数调用分配给适当的名称变量来解决问题。感谢@Oriol 下面的帮助!

var rafael_bravo = addFamilyMember("rafael_bravo", "Rafael Bravo", "-", "-", "-", god, "GeneralPartner");
var calixta_otero = addFamilyMember("calixta_otero", "Calixta Otero", "-", "-", "-", rafael_bravo, "LimitedPartner");
var omar_bravo = addFamilyMember("omar_bravo", "Omar Bravo", "-", "-", "-", rafael_bravo, "LimitedPartner");


    function addFamilyMember (child,name,dob,dod,img,parent,link) {

                    child = new primitives.orgdiagram.ItemConfig();
                    child.name = name;
                    child.birth = dob;
                    child.death = dod;
                    child.image = "includes/images/" + img + ".png";
                    child.templateName = "familyTemplate";
                    parent.items.push(child);

                    jQuery(".basicdiagram").orgDiagram("update", primitives.orgdiagram.UpdateMode.Refresh);
                    return child;
                }
4

1 回答 1

1

你可以使用

var names = {};
function addFamilyMember (id,name,dob,dod,img,parentId,link) {
    var child = new primitives.orgdiagram.ItemConfig();
    child.name = name;
    child.birth = dob;
    child.death = dod;
    child.image = img ? "includes/images/" + img + ".png" : "";
    child.templateName = "familyTemplate";
    names[id] = child;
    names[parentId].items.push(child);
    jQuery(".basicdiagram").orgDiagram("update", primitives.orgdiagram.UpdateMode.Refresh);
    return child;
}
addFamilyMember("rafael_bravo", "Rafael Bravo", "-", "-", "", "god", "GeneralPartner");
addFamilyMember("calixta_otero", "Calixta Otero", "-", "-", "", "rafael_bravo", "LimitedPartner");
addFamilyMember("omar_bravo", "Omar Bravo", "-", "-", "", "rafael_bravo", "LimitedPartner");

另请注意,如果您使用"-"as img,浏览器将尝试下载includes/images/-.png,因此最好使用虚假的内容(如空字符串""、 or void(0)、 or false)然后检查它。我使用了一个空字符串,但一些浏览器会尝试将当前页面下载为图像,因此最好child.image在创建图像时检查。

于 2013-10-06T02:37:32.110 回答