现有框架中的 Javascript Ajax 原型
我目前在 ASP.NET MVC 中编写了大量代码,并大量使用 jQuery 对返回 json 的操作进行 ajax 调用。
我已经开始发展这种模式,在全局对象下创建一个对象,其中包含“成功”和“失败”回调以及调用核心逻辑并接受参数的“go”方法...如下
var g1 = {}; // global object for some project...
g1.loadFileList = {
success: function (data, status, request) {
// notify success - or do nothing
},
fail: function (request, status, err) {
// notify #FAIL!
},
go : function (args) {
$.ajax({
url: '/Home/SomethingInterest',
type: 'POST',
success: this.success,
error: this.fail
});
}
};
$(document).ready(function() { g1.loadFileList.go({ whatever = 'data' }); });
:这样做是为了让我的想法井井有条,我正要开始研究一种稍微不同的模式,我可以开始原型设计以重用我在错误和成功处理程序中所做的一些日志记录,但后来我想...这是在其他一些 JS Fx???
是吗?欢迎任何关于执行类似操作的现有框架的想法或关于如何更好地构建原型的想法。
I currently write quite a bit of code in ASP.NET MVC and use jQuery quite a bit also to make ajax calls to Actions that return json.
I've started to evolve this pattern where I make an object below my global object that contains a 'success' and 'fail' callback along with a 'go' method that invokes the core logic and takes arguments... as follows:
var g1 = {}; // global object for some project...
g1.loadFileList = {
success: function (data, status, request) {
// notify success - or do nothing
},
fail: function (request, status, err) {
// notify #FAIL!
},
go : function (args) {
$.ajax({
url: '/Home/SomethingInterest',
type: 'POST',
success: this.success,
error: this.fail
});
}
};
$(document).ready(function() { g1.loadFileList.go({ whatever = 'data' }); });
I do this to keep my thoughts organized and I was about to start working on a slightly different pattern that I could start prototyping to reuse some of the logging I am doing in the error and success handlers, but then I thought... Is this in some other JS Fx???
Is it? Any thoughts on pre-existing frameworks that do similar things or thoughts on how to prototype this better is welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我要把这归因于。这只是我有时用来封装异步调用及其成功和失败响应处理程序的一种模式。
I'm going to chalk this up to. This is just a pattern that I use sometime to encapsulate the async call with its success and fail response handlers.