开发者

Javascript Ajax prototype in existing Framework

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 wor开发者_StackOverflow中文版king 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜