0
我有使用JSON呼叫到MVC的控制器,如下面填充的表的方法,包括:的getJSON內部事件上傳入的函數調用遇到引用錯誤
pub.PopulateTable = function (args) {
var page = 1,
// ...variables snipped...
OnRowClicked;
// Unpack arguments...
if (args != null) {
// ...details snipped...
OnRowClicked = args.OnRowClicked;
}
// Build path...
path = path + controller + '/' + action + '/';
if (actionId != null && actionId != '') {
path = path + actionId + '/';
}
path = path + page;
$.getJSON(path, function (data) {
if (data.Values.length > 0) {
// Clear table body, then inject list...
$tableBody.html('');
$.tmpl($template, data.Values).appendTo($tableBody);
// ...snip various instructions, for brevity...
// Add on-click returning...
$('tr').click(function() {
var $row = $(this),
rowData = {};
rowData.SomeProperty = $row.children('#IdColumn').val();
$modalDialog.modal('hide');
OnRowClicked(rowData); // Problem!!!
});
} else {
$tableBody.html("<tr><td colspan=2>No entries...</td></tr>");
}
});
也許是因爲的getJSON是一個異步操作,但是通過方法參數對象傳入的OnRowClicked()方法遇到引用錯誤時它會嘗試使用被傳遞給它下面的(簡單)方法:當我打通
function textFieldRowClickHandler(rowData) {
$myTextFieldHere.val(rowData.SomeProperty);
}
對話框(這會導致PopulateTable運行和綁定裏面的事件),並選擇一個記錄(因此觸發點擊事件),我不斷收到一個引用錯誤,因爲rowData.SomeProperty是未定義的,即使回調將一個click事件綁定到每個tr標籤,當它被點擊,關閉對話框,獲取值,構建一個對象,並將其傳遞給給定的方法。
如上所述 - 我知道getJSON是一個異步操作,這就是我認爲我的問題正在出現的地方 - 我對Async範例並不熟悉。我做錯了什麼,確切地說?
我沒有看到比rowData什麼你的代碼錯誤,其他沒有按'沒有'SomeProperty'屬性,它只有'ID' –
eep!接得好!但我的意思是SomeProperty,而不是ID。編輯:更新了代碼;我確信我的代碼有正確的變量設置,只是爲了排除。 –