现有框架中的 Javascript Ajax 原型

发布于 2024-11-18 03:39:32 字数 844 浏览 5 评论 0原文

我目前在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠意 2024-11-25 03:39:32

我要把这归因于。这只是我有时用来封装异步调用及其成功和失败响应处理程序的一种模式。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文