Below is the basic shell of an application I am working on that is part of a Webhosting Control Panel. This part is for the DNS records management.
So in my code below, I have taken away all the main functionality as it's not relevant to my question and to make it less cluttered.
On the page I have between 12-15 JavaScript Events
ranging from click events
to keypress event
to keydown events
etc...
Right now I have these main functions...
dnsRecords.init()
dnsRecords.events.init()
dnsRecords.records.addRow(row)
dnsRecords.records.save()
dnsRecords.records.undoRow(row)
dnsRecords.records.deleteRow(row)
I have put all my Events
into dnsRecords.events.init()
. So each Event
basically calls or passes Data to the dnsRecords.records
functions.
Since I am new to JavaScript I am wanting to know if there is anything really wrong with this method or is there is a better location or way to put all those different Events?
Is it generally a good idea or not to put all my Events
into 1 area like that and then have the fire of other functions instead of cluttering there callback area with logic code?
Also note, I am not looking to use Backbone or some other Framework at this time, just want to know a good way to structure a small single page application like this. Thank you.
var dnsRecords = {
unsavedChanges : false,
init: function() {
dnsRecords.events.init();
},
events: {
init: function() {
// If user trys to leave the page with UN-SAVED changes, we will Alert them to...
$(window).on('beforeunload',dnsRecords.events.promptBeforeClose);
$(document).on('keypress','#typeMX div.hostName > input',function() {
$(document).off('keypress','#typeMX div.hostName > input');
});
// Activate SAVE and UNDO Buttons when Record Row EDITED
$(document).on("keydown", "#dnsRecords input" ,function() {
});
// Add new Record Row
$("#dnsRecords div.add > .btn").click(function(e) {
e.preventDefault();
});
// Mark Record as "Deleted" and change the view of it's Row to reflect a Deleted item
$(document).on("click", ".delete" ,function(e) {
dnsRecords.records.deleteRow($(this));
e.preventDefault();
});
// Show Undo button when editing an EXISTING ROW
$(document).on("keydown","div.dnsRecord input[type='text']",function() {
});
// Undo editing of an EXISTING ROW
$("button.undo").on("click",function() {
dnsRecords.records.undoRow($(this));
});
//Save Changes
$("#dnsTitle a.save").click(function() {
zPanel.loader.showLoader();
});
//Undo ALL Record Type Changes
$("#dnsTitle a.undo").click(function() {
});
$("form").submit(function() {
});
},
},
records: {
addRow: function(record) {
// All the code to Add a record here
},
save: function() {
// All the code to Save a record here
},
undoRow: function(row) {
// All the code to Undo a record here
},
deleteRow: function(row) {
// All the code to Delete a record here
dnsRecords.unsavedChanges = true;
},
}
};
$(function(){
dnsRecords.init();
});
dnsRecords
stuff? which parts? – Joseph the Dreamer Apr 24 at 5:26