ASP.NET MVC 3 - 启用不显眼的 JavaScript,ajaxComplete 触发一次,再也不会
在使用 unobtrusive javascript 之前,我使用以下寄存器处理 ajax 完成事件:
Sys.Net.WebRequestManager.add_completedRequest(myHandler);
每次 ajax 请求完成时都会触发此事件处理程序。我还在 $(document).ready() 中绑定了一个 ajaxComplete 事件,专门通过 jQuery 处理 ajax 调用:
$.ajaxComplete(function (e, xhr, settings) {
myHandler(xhr)
});
这也很好用。但是,当我在 ASP.NET MVC 3 中启用不显眼的 javascript 时,我得到了不同的行为。它在第一个 ajax 调用完成时第一次触发,但在后续的 ajax 请求中,ajaxComplete 事件再也不会触发。
现在,我知道您需要在通过部分回发更新表单中的元素后调用 $.validate.unobtrusive.parse() 来重建验证。我需要做类似的事情来确保 ajaxComplete 可以在后续请求中再次触发吗?我找不到这方面的文档。
仅供参考:我已经包含了所有 jquery*.js 库来支持不显眼的 javascript。我还包含 MicrosoftMvc*.js 库来支持项目中的遗留代码。我本来希望将一切都转换过来,直到遇到这个问题。
Before unobtrusive javascript, I handled ajax complete events with the following register:
Sys.Net.WebRequestManager.add_completedRequest(myHandler);
This event handler will fire every time an ajax request is complete. I also have an ajaxComplete event bind in $(document).ready(), to handle ajax calls exclusively through jQuery:
$.ajaxComplete(function (e, xhr, settings) {
myHandler(xhr)
});
Which also works great. But I get a different behavior when I enabled unobstrusive javascript in ASP.NET MVC 3. It fires the first time when the first ajax call is complete, but on subsequent ajax requests, ajaxComplete event never fires again.
Now, I know about that you need to call $.validate.unobtrusive.parse() to rebuild the validation after the elements in the form updated via partial postback. Is there something similar that I need to do to make sure that ajaxComplete can fire again on subsequent requests? I cannot find the documentation on this.
FYI: I have included all the jquery*.js libs to support unobtrusive javascript. I also have the MicrosoftMvc*.js libs included to support legacy code in the project. I was hoping to convert everything over until I ran into this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您实际上需要将 ajaxComplete 附加到我认为的元素吗?
您应该只需要在元素的生命周期中将 ajaxComplete 附加到元素一次。
You need to actually attach ajaxComplete to an element I thought?
You should only need to attach ajaxComplete to an element once in its lifetime.