我有JSON對象;讓我們稱之爲模型;它有2個類型數組的屬性。該模型還有一個函數「hasAnyFilterSelected」,用於檢查是否有任何數組屬性被填充。我在UI上有下拉列表的搜索按鈕。當用戶點擊搜索按鈕時,會出現以下步驟:將函數添加到JSON對象
1>將所選值從下拉列表中推送到模型的數組 屬性中。
2> model.hasAnyFilterSelected()檢查數組中是否有值,在調試器中我看到模型中填充了值。
3>如果是,則刷新通過執行datasource.read()
4>劍道格稱爲「getFilters」方法,該方法在調試器我看到模型中填充值再次返回模式,劍術網格。
5>然後不知何故 hasAnyFilterSelected()被再次調用,這一次模型陣列屬性是不確定的。它看起來像劍道試圖再次序列化模型,這次模型有不同的實例,但我不知道。
$(function() {
var model = {
selectedTaxYears: [],
selectedStates: [],
hasAnyFilterSelected: function() {
return !(this.selectedTaxYears.length == 0 &&
this.selectedStates.length == 0)
}
};
$_btnSearch.click(function() {
pushFilters();
if (model.hasAnyFilterSelected()) // this returns true when array is populated. In debugger I could see its populated
{
$(gridID).data("kendoGrid").dataSource.read();
// fails at above line when dataSource reads
// it calls getFilters method as expected, I see model is populated.
// then it calls hasAnyFilterSelected() function again, (don’t know why) and at this time selectedTaxYears & selectedStates is undefined, so it fails
// In debugger I DO NOT see model is populated. So looks like different instance of model
$(gridID).data('kendoGrid').refresh();
}
});
function pushFilters() {
model.selectedTaxYears = $_taxYears.val();
model.selectedStates = $_stateProvience.val();
}
function getFilters() {
return model;
}
var dataSource = $_grid.data("kendoGrid").dataSource;
dataSource.transport.options.read.data = getFilters;
})
我想知道爲什麼會發生這種情況?我是否正確定義了JSON函數?那是首選方法?