代码之家  ›  专栏  ›  技术社区  ›  chris

dojo.connect/dojo.hitch scope问题?

  •  0
  • chris  · 技术社区  · 16 年前

    我有一个程序员类,它用项目名和复选框填充ul——当单击复选框时,弹出对话框应该显示程序员id和项目名。connect应该为每个li设置onclick,但项目(i)默认为最后一个值(windows)。你知道为什么会这样吗?

    ...
    projects: {"redial", "cms", "android", "windows"},
    name: "Chris",
    id: "2",
    
    constructor: function(programmer) {
        this.name = programmer.name;
        this.id = programmer.id;
        this.projects = programmer.projects;
    },
    
    update: function(theid, project) {
        alert(theid + ", " + project);
    },
    
    postCreate: function() {
        this.render();
    
        // add in the name of the programmer
        this.programmerName.innerHTML = this.name;
    
        for(var i in this.projects) {
            node = document.createElement("li");
            this.programmerProjects.appendChild(node);
            innerNode = document.createElement("label");
            innerNode.setAttribute("for", this.id + "_" + i);
            innerNode.innerHTML = i;
            node.appendChild(innerNode);
            tickNode = document.createElement("input");
            tickNode.setAttribute("type", "checkbox");
            tickNode.setAttribute("id", this.id + "_" + i);
            if(this.projects[i] == 1) {
                tickNode.setAttribute("checked", "checked");
            }
    
            dojo.connect(tickNode, 'onclick', dojo.hitch(this, function() {  
                this.update(this.id, i)
            }));
            node.appendChild(tickNode);
        }   
    },  
    
    3 回复  |  直到 16 年前
        1
  •  3
  •   ChrisChris    16 年前

    刚刚发现附加参数可以附加到挂接装置上:

    dojo.connect(tickNode, 'onclick', dojo.hitch(this, function() {  
                this.update(this.id, i)
    }));
    

    应该是:

    dojo.connect(tickNode, 'onclick', dojo.hitch(this, "update", this.id, i));
    
        2
  •  0
  •   Kyle LeNeau    16 年前

    为什么要调用此.render()?这是您的功能还是小部件库(即已经在生命周期中)?为了获得良好的度量,请确保在后创建中调用this.inherited(参数)。

    我的猜测是tickNode还不在DOM中以便连接工作。在设置连接之前,请尝试附加复选框。最后一个正在被解雇,因为它是被引用持有的。你可以试试这样的方法:

    for(var i = 0; i < this.projects.length; i++) {
        var p = this.projects[i];
        node = document.createElement("li");
        this.programmerProjects.appendChild(node);
        innerNode = document.createElement("label");
        innerNode.setAttribute("for", this.id + "_" + p);
        innerNode.innerHTML = p;
        node.appendChild(innerNode);
        tickNode = document.createElement("input");
        tickNode.setAttribute("type", "checkbox");
        tickNode.setAttribute("id", this.id + "_" + p);
        if(i == 0) { //first item checked?
            tickNode.setAttribute("checked", "checked");
        }
    
        node.appendChild(tickNode);
    
        dojo.connect(tickNode, 'onclick', function(e) {
            dojo.stopEvent(e);
            this.update(this.id, p);
        });
    }
    

    我也会考虑研究dojo.create,而不是createElement。祝你好运!

        3
  •  0
  •   Justin Johnson    16 年前

    或者,我认为它更干净,您可以将上下文作为第三个参数传递给dojo.connect:

    dojo.connect(tickNode, 'onclick', this, function() {  
      this.update(this.id, i);
    });