Search code examples
javascriptjqueryajaxtheory

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 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.


Solution

  • 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.