Question: Which is the better data format for a JSON. The requirement is to be able to store and retrieve information about how many projects are deployed on to a server. 

Object-Based Design

```
{ 
   "Server1":{
        "project1":{
            "buildNo":"290",
            "deployed":"12/12/2012"
        },
        "project2":{
            "buildNo":"291",
            "deployed":"11/12/2012"
        },
        "project3":{
            "buildNo":"209",
            "deployed":"11/12/2012"
        }
  }
}

```

Array-Based Design

```
{ 
   "Server1":[
        {"project1":{
            "buildNo":"290",
            "deployed":"12/12/2012"
        }},
        {"project2":{
            "buildNo":"291",
            "deployed":"11/12/2012"
        }},
        {"project3":{
            "buildNo":"209",
            "deployed":"11/12/2012"
        }}
    ]
}

```

Please do let me know your thoughts for or against either of these approaches.

Answer: 
Looks like jQuery's promises can help you out here:

```
var templatePromise = $.get({
    url: "/my/remote/template"
});

templatePromise.done(function(template) {
     // Add initial things using remote template.
});

```

and elsewhere you can do:

```
$.get({
    url: "/something/that/requires/an/asynchronous/callback",
    success: function () {
        templatePromise.done(function(template) {
              // Do more things using remote template.
        });
    }
});

```

Usually `$.get` (and `$.ajax`, etc) are used with `success:` and `error:` callbacks in the initial invocation, but they also return a promise object that acts just like a $.Deferred, documented here: <http://api.jquery.com/category/deferred-object/> which allows you to do what you're asking. For error handling, you can use `templatePromise.fail(...)` or simply add `error: ...` to the initial `$.get`.

In general it's best to avoid synchronous AJAX calls because most browsers' interfaces will block while the HTTP request is being processed.
If I understand correctly, jQuery will do what you want of it by way of Deferreds/promises.

You can even generalise the remote template fetcher by

* using a js plain object in which to cache any number of templates
* renaming the function and passing a url to it, `get_remote_template(url)`

js :

```
var template_cache = {};//generalised template cache

//generalised template fetcher
function get_remote_template(url) {
    var dfrd = $.Deferred();
    if(!template_cache[url]) {
        $.get(url).done(function(tpl) {
            template_cache[url] = tpl; //we use the url as a key.
            dfrd.resolve(tpl);
        });
    }
    else {
        dfrd.resolve(template_cache[url]);
    }
    return dfrd.promise();
}

```

Then :

```
var url1 = "/my/remote/template"; //the url of a particular template

get_remote_template(url1).done(function(tpl) {
    // Add initial things using tpl.
});

```

And, earlier or later :

```
$.get({
    url: "/something/that/requires/an/asynchronous/callback",
    success: function(data) {
        init_remote_template(url1).done(function (tpl) {
            // Do something using tpl (and data).
        });
    }
});

```

Note how `get_remote_template()` returns a promise. If the requested template is already cached, the promise is returned ready-resolved. If the template is not yet in cache (ie. it needs to be downloaded from the server), then the promise will be resolved some short time later. Either way, the fact that a promise is returned allows a `.done()` command to be chained, and for the appropriate template to be accessed and used.

EDIT
----

Taking @SophieAlpert's points on board, this version caches the promise associated with a tpl rather than the tpl itself.

```
var template_cache = {};//generalised cache of promises associated with templates.

//generalised template fetcher
function get_remote_template(url) {
    if(!template_cache[url]) {
        template_cache[url] = $.get(url);
    }
    return template_cache[url];//this is a promise
}

```

This version of `get_remote_template()` would be used in the same way as above.