I am slowly learning to master JavaScript and particularly the art of self-executing/invoking functions. I have developed a simple JavaScript plugin and I think I have followed the correct standards.
It is pretty basic in what it does, it is to store information on a oData BATCH request, which provides functions to add to the batch request, get the string representation (which can then be used in a HTTP request), and I am expanding on it as we speak.
However, the code is as follows. It was using jQuery but not any more due to resource constraint.
I am generally after comments on what could have been done better. I am doing this in an attempt to reduce the window namespace. Therefore BatchInfo and BatchCacheRequest (which are a bit more specific to my code) are not clogging up the name space, nor do I have to keep an array anywhere do hold the BatchInfo objects.
(function () {
var info = [],
b = {
clear: function () {
info = [];
},
get: function () {
return info;
},
add: function (command, url, data) {
info.push(new BatchInfo(command, url, data));
return info;
},
toString: function () {
var batchString = ['--batchfull'];
batchString.push('Content-Type: multipart/mixed; boundary=batchitems');
for (var i = 0; i < info.length; i++) {
batchString.push('');
batchString.push('--batchitems');
batchString.push('Content-Type: application/http');
batchString.push('Content-Transfer-Encoding: binary');
batchString.push('');
batchString.push(info[i].Command + ' ' + info[i].URL + ' HTTP/1.1');
if (info[i].Command !== 'DELETE') {
batchString.push('Content-Type: application/json;type=entry');
}
batchString.push('');
batchString.push(info[i].Data);
}
return batchString.join('\r\n') || '';
}
};
function BatchInfo(command, url, data) {
this.URL = url || '';
this.Command = command || '';
this.Data = data || '';
}
function BatchCacheRequest(info, description) {
this.Info = info || '';
this.Description = description || '';
this.SubmitDateTime = new Date().toString('yyyy-MM-dd HH:mm');
}
window.batch = b;
})();
Usage is:
batch.add('POST', 'http://www.mywebsite.com', '[some data to post]');
Then toString()
will return me a string which is formatted like a BATCH request (which I will use later on in the code).
Is there anything I'm doing stupidly bad here?