function ContactList(){
var objSelf = this;
this.List = $( "#contact-list" );
this.DOMReferences = {
Template: this.List.find( "tbody.dom-template" ),
Content: this.List.find( "tbody.table-content" )
};
$( document ).bind(
"contactsUpdated",
function(){
objSelf.GetContacts();
}
);
this.GetContacts();
}
ContactList.prototype.DeleteContact = function( ID ){
var objSelf = this;
$.ajax(
{
type: "get",
url: "./api/Contacts.cfc",
data: {
method: "DeleteContact",
id: ID
},
dataType: "json",
success: function( objResponse ){
if (objResponse.SUCCESS){
$( document ).trigger( "contactsUpdated" );
} else {
objSelf.ShowErrors( objResponse.ERRORS );
}
},
error: function( objRequest, strError ){
objSelf.ShowErrors( [ "An unknown connection error occurred." ] );
}
}
);
}
ContactList.prototype.GetContacts = function(){
var objSelf = this;
$.ajax(
{
type: "get",
url: "./api/Contacts.cfc",
data: {
method: "GetContacts"
},
dataType: "json",
success: function( objResponse ){
if (objResponse.SUCCESS){
objSelf.ShowContacts( objResponse.DATA );
} else {
objSelf.ShowErrors( objResponse.ERRORS );
}
},
error: function( objRequest, strError ){
objSelf.ShowErrors( [ "An unknown connection error occurred." ] );
}
}
);
}
ContactList.prototype.ShowContacts = function( arrContacts ){
var objSelf = this;
var arrRows = $.map(
arrContacts,
function( objContact, intIndex ){
var jRow = objSelf.DOMReferences.Template.children( "tr" ).clone();
jRow.attr( "id", objContact.ID );
jRow.find( "td.name-column" ).text( objContact.NAME );
jRow.find( "td.hair-column" ).text( objContact.HAIR );
jRow.find( "td.actions-column a" )
.attr( "href", "javascript:void( 0 )" )
.click(
function( objEvent ){
objSelf.DeleteContact( objContact.ID );
return( false );
}
)
;
return( jRow.get( 0 ) );
}
);
this.DOMReferences.Content
.empty()
.append( arrRows )
;
}
ContactList.prototype.ShowErrors = function( arrErrors ){
var strError = "Please review the following:\n";
$.each(
arrErrors,
function( intI, strValue ){
strError += ("\n- " + strValue);
}
);
alert( strError );
}
$(
function(){
var objContactList = new ContactList();
}
);