Example of a javascript module with public and private functions
var FooterData = (function(){
var createFooter = function($divRef,edit){
//create table
tbl = document.createElement('table');
//tbl.style.width = '50%';
tbl.style.border = '1px solid black';
tbl.setAttribute('id','footerTable');
var trHeaderRow2 = tbl.insertRow();
var b = document.createElement('button');
b.setAttribute('type', 'button');
b.innerHTML = 'Save';
b.id = "cmdSave"
createCell(trHeaderRow2,b,'cmdSaveTD');
var b = document.createElement('button');
b.setAttribute('type', 'button');
b.innerHTML = 'Cancel';
b.id = "cmdCancel"
createCell(trHeaderRow2,b,'cmdCancelTD');
$divRef.append(document.createElement('br'));
$divRef.append(tbl);
if(edit){
Save.saveEditAction();
}
else{
Save.saveAction();
}
};
function createCell(row, cellContents, id)
{
var td = row.insertCell();
td.appendChild(cellContents);
td.style.border = '1px solid black';
td.id = id;
return td;
}//end createRow
return{
createFooter:createFooter
};
})();
Test.publicMethod();
var Test = (function (){
//private method
var testItem = function(test){
alert(test);
};
//public accessor
return {
publicMethod:testItem('in the private function again')
};
})();
var addAttributes = (function (){
var checkLength = function(o,n,min,max){
if ( o.val().length > max || o.val().length < min ) {
o.addClass( "ui-state-error" );
updateTips( "Length of " + n + " must be between " +
min + " and " + max + "." );
return false;
} else {
return true;
}
};
var updateTips = function(t){
this.tips
.text(t)
.addClass( "ui-state-highlight" );
setTimeout(function() {
tips.removeClass( "ui-state-highlight", 1500 );
}, 500 );
};
var addAttribute = function(){
//this.allFields = $([]).add($('#addAttribute'));
this.tips = $(".validateTips");
var valid = true;
var addAttribute = $('#addAttribute');
addAttribute.removeClass("ui-state-error");
valid = checkLength( addAttribute, "Attribute", 3, 25 );
if (valid) {
alert('valid entry');
dialog.dialog("close");
}
return valid;
};
return{
addAttrib: addAttribute
};
})();
var Module = (function () {
var privateMethod = function (message) {
console.log(message);
};
var publicMethod = function (text) {
privateMethod(text);
};
return {
publicMethod: publicMethod
};
})();
// Example of passing data into a private method
// the private method will then `console.log()` 'Hello!'
Module.publicMethod('Hello!');