Я пытаюсь добавить флажок в заголовке для выбора всех строк в jsgrid, как показано здесь . Работает нормально. Но вместо того, чтобы поместить весь код на одной странице, я попытался поместить эти функции в IIFE следующим образом.
var jsgridCheckBox = (function() {
var selectedItems = [];
var selectItem = function(item) {
selectedItems.push(item);
...
};
var unselectItem = function(item) {
selectedItems = $.grep(selectedItems, function(i) {
return i !== item;
});
...
};
var selectAllCheckBox = function(item) {
selectedItems = [];
if (this.checked) {
...
}
};
return {
selectedItems: selectedItems,
selectItem: selectItem,
unselectItem: unselectItem,
selectAllCheckBox: selectAllCheckBox
}
})();
В JSGrid,
$(function() {
$("#jsGrid").jsGrid(
...
fields: [
itemTemplate: function(_, item) {
return $("<input>").attr("type", "checkbox").attr("class", "singleCheckbox")
.prop("checked", $.inArray(item, jsgridCheckBox.selectedItems) > -1)
.on("change", function() {
$(this).is(":checked") ? jsgridCheckBox.selectItem(item) : jsgridCheckBox.unselectItem(item);
});
}
...
]
});
});
И после выбора флажка (ов), когда я пытаюсь вызвать его, как показано ниже
var dialog = $("#dialog-form").dialog({
buttons: {
"Reject": rejectRequest,
}
});
function rejectRequest() {
alert(jsgridCheckBox.selectedItems.length);
for (var i = 0; i < jsgridCheckBox.selectedItems.length; i++) {
alert(jsgridCheckBox.selectedItems[i].some_id);
...
}
...
}
всегда возвращает 0
. Это то, что jsgridCheckBox
всегда возвращает свежий экземпляр? Как я могу поддерживать состояние массива в IIFE?