2013-04-15 106 views
0

我遇到了點擊事件不能使用Javascript MVC控制器的問題。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,我有一個應該工作的點擊功能。但事實並非如此。正在調用構造函數並正確地追蹤元素。任何想法爲什麼點擊事件不起作用?

回答

0

我假設assignViewLink元素被創建並附加在成功回調中。如果是這樣,它看起來像一個序列問題。在綁定click事件時,assignmentViewLink元素尚未創建,因此,未附加單擊事件處理程序。

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

要驗證這一點,請將elements.assignmentViewLink(...)移入成功回調。

+0

嘗試過,仍然沒有運氣。元素在到達click事件之前已定義。我甚至嘗試過一個'實時'點擊事件,但這也不起作用。 – pfunc

相關問題