我有一个Kendo视图模型,我正在尝试检测页面上的用户更改,以便我可以警告他们,如果他们即将离开而不保存(使用onbeforeunload事件处理程序)。我绑定到视图模型更改事件以将“已更改”布尔值设置为true。但是,在document.ready完成之后,许多视图模型字段从其数据源异步加载,触发我无法区分用户更改的更改事件。有没有办法检测所有字段何时完成检索数据,以便我不会将其绑定更新误认为用户更改?
<script>
var myViewModel = kendo.observable({
someField: "testing",
//...Other fields with data sources that get filled in asynchronously
//Problem is that these are bound to fields in html and trigger change event
changed: false,
loading: false,
fetch: function () {
this.set("loading", true);
//...
this.set("loading", false);
}
});
</script>
<div id="myView">
<input type="text" data-bind="value: someField" class="k-textbox" />
<!--Other fields bound to fields in myViewModel-->
</div>
<script>
$(document).ready(function () {
myViewModel.fetch().then(function () {
kendo.bind($("#myView"), myViewModel);
}, function (error) {
});
});
myViewModel.bind("change", function (e) {
if (myViewModel.loading === false) {
myViewModel.changed = true;
}
});
window.onbeforeunload = function () {
if (myViewModel.changed) {
return "You haven't saved your changes!";
}
}
</script>