AddThis Social Bookmark Button
Let's have a look at the code below, roll over the comments if you want to see the comments associated with the nearby code (be sure you have javascript enabled for this to work properly).

// We define the font family and the size for all the components
_global.style.setStyle("fontSize", 11);
_global.style.setStyle("fontFamily", "Verdana");

// We don't want the scroll bar to be visible in the list component unless it is needed.
entries.vScrollPolicy = "Auto";

// We create or retrieve the shared object containing the data of our address book
myAddresBook= SharedObject.getLocal("myAddresBook");

// We check if the shared object exists, if not we create the arrays that will store the data
if (myAddresBook.data.created == undefined) {
myAddresBook.data.firstnameSO = new Array();
myAddresBook.data.nameSO = new Array();
myAddresBook.data.addressSO = new Array();
myAddresBook.data.emailSO = new Array();
myAddresBook.data.created = 1;
myAddresBook.flush();
// if the shared object exists we just need to populate the list component with the data
} else {
populateList();
}

// when the NEW button is pressed we create a new entry
newButton.onRelease = function() {
entries.addItem({label:"New entry", firstnameData:"", nameData:"", addressData:"", emailData:""});
entries.selectedIndex = entries.length-1;
};

// when the ADD button is pressed we populate the selected item of the list with the according data
addButton.onRelease = function() {
// we use the firstname as a label
entries.selectedItem.label = firstnameField.text;
// we assign the data to the selected item and we push that data in the according array in the shared object
entries.selectedItem.firstnameData = firstnameField.text;
myAddresBook.data.firstnameSO.push(firstnameField.text);
entries.selectedItem.nameData = nameField.text;
myAddresBook.data.nameSO.push(nameField.text);
entries.selectedItem.addressData = addressField.text;
myAddresBook.data.addressSO.push(addressField.text);
entries.selectedItem.emailData = emailField.text;
myAddresBook.data.emailSO.push(emailField.text);
entries.selectedIndex = entries.selectedIndex;
myAddresBook.flush();
};

// when the DELETE button we delete the data from the according arrays in the shared object andd we remove the item from the list
deleteButton.onRelease = function() {
myAddresBook.data.firstnameSO.splice(entries.selectedIndex,1) ;
myAddresBook.data.nameSO.splice(entries.selectedIndex,1);
myAddresBook.data.addressSO.splice(entries.selectedIndex,1) ;
myAddresBook.data.emailSO.splice(entries.selectedIndex,1);
entries.removeItemAt(entries.selectedIndex);
myAddresBook.flush();
emptyFields();
};

// each time we select an element from the list the data populates the text fields on the right
listenerObject = new Object();
listenerObject.change = function() {
firstnameField.text = entries.selectedItem.firstnameData;
nameField.text = entries.selectedItem.nameData;
addressField.text = entries.selectedItem.addressData;
emailField.text = entries.selectedItem.emailData;
};
entries.addEventListener("change", listenerObject);



// this little function below populate the list with the data of the shared object
function populateList() {
for (var i = 0; i<=myAddresBook.data.firstnameSO.length-1; i++) {
entries.addItem({label:myAddresBook.data.firstnameSO[i], firstnameData:myAddresBook.data.firstnameSO[i], nameData:myAddresBook.data.nameSO[i], addressData:myAddresBook.data.addressSO[i], emailData:myAddresBook.data.emailSO[i]});
}
}

// the function below empty the fields on the right each time an item is removed from the list

function emptyFields(){
firstnameField.text = "";
nameField.text = "";
addressField.text = "";
emailField.text = "";
}

Good luck :)



AddThis Social Bookmark Button
If you think this page is providing useful information, don't hesitate to leave a comment.
flashvalley
 
Copyright ©2006-2008 flashvalley.com - All rights reserved