Javascript MVC控制器和Click事件

时间:2013-04-15 15:02:46

标签: javascript javascriptmvc

我遇到使用Javascript MVC控制器无效的点击事件的问题。

TEST.Assignments.AssignmentsController = function (element) {

var elements = {
    activeAssignmentsPanel: $('#lpn-activeAssignments_Cont'),
    assignmentViewLink: $("#lpn-activeAssignments_Cont table tr th a")
};

var _this = this;
var model = new TEST.Assignments.AssignmentModel();

this.buildAssignmentsList = function () {
    var assignments = model.getActiveAssignmentsList({
        assignmentMode: "active",
        mock: true,
        success: function (data) {
                dust.render("ActiveAssignmentsPanel", data, function(err, out) {
                elements.activeAssignmentsPanel.append(out);
            });
        }
    });
};

this.getAssignmentDetails = function(assignmentId) {
    console.log(assignmentId);
};

//bind all events
elements.assignmentViewLink.click(function (e) {
    console.log("blah");
    console.log($(this).data("assignmentKey"));
});


};//end assignments controller


$(function () {
    var assignmentsController = new TEST.Assignments.AssignmentsController();
    assignmentsController.buildAssignmentsList();
});

如果你看一下//bind events,那我就有一个应该有效的点击功能。但事实并非如此。正在调用构造函数并正确地跟踪元素。知道为什么click事件不起作用吗?

1 个答案:

答案 0 :(得分:0)

我假设在成功回调中创建并附加了assignmentViewLink元素。如果是这样,它看起来像序列问题。绑定click事件时,尚未创建assignmentViewLink元素,因此未附加click事件处理程序。

//bind all events
// assignmentViewLink is empty []
elements.assignmentViewLink.click(function (e) {
  console.log("blah");
  console.log($(this).data("assignmentKey"));
});

要验证这一点,请将elements.assignmentViewLink(...)移动到成功回调中。