0

我遇到了点击事件无法使用 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,我有一个应该可以工作的点击功能。但事实并非如此。构造函数被调用并且元素被正确追踪。知道为什么单击事件不起作用吗?

4

1 回答 1

0

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

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

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

于 2013-04-15T15:23:08.593 回答