var PetView = (function () { var dao; // Referencia a this que permite acceder a las funciones públicas desde las funciones de jQuery. var self; var peopleID; var formId = 'people-form'; var listId = 'people-list'; var formQuery = '#' + formId; var listQuery = '#' + listId; function PetView(petDao, formContainerId, listContainerId, paramID) { peopleID = paramID; dao = petDao; self = this; this.init = function () { var cont = 0; var humans = [] var humans_list = [] var daoPeople = new PeopleDAO(); function Human(id, name, surname) { this.id = id; this.name = name; this.surname = surname; } daoPeople.listPeopleSync(function (people) { $.each(people, function (key, human) { humans[human.id] = new Human(human.id, human.name, human.surname); humans_list[cont] = new Human(human.id, human.name, human.surname); cont++; }); }, function () { alert('No has sido posible acceder al listado de mascotas.'); }); insertPetForm($('#' + formContainerId), humans_list, humans); insertPetList($('#' + listContainerId)); //Opción en el caso de que se quieran listar todas las mascotas de todos los dueños if (peopleID === "all") { dao.listAll(function (people) { $.each(people, function (key, pet) { appendToTable(pet, humans); }); }, function () { alert('No has sido posible acceder al listado de mascotas.'); }); } else { //En caso de que solo se quiera listar las mascotas de una persona dao.listPetsByPeopleID(peopleID, function (people) { $.each(people, function (key, pet) { appendToTable(pet, humans); }); }, function () { alert('No has sido posible acceder al listado de mascotas.'); }); } // La acción por defecto de enviar formulario (submit) se sobreescribe // para que el envío sea a través de AJAX $(formQuery).submit(function (event) { var pet = self.getPetInForm(); if (self.isEditing()) { dao.modifyPet(pet, function (pet) { $('#person-' + pet.id + ' td.name').text(pet.name); $('#person-' + pet.id + ' td.surname').text(humans[pet.peopleID].name + " " + humans[pet.peopleID].surname); $('#person-' + pet.id + ' td.pet_type').text(pet.type); self.resetForm(); }, showErrorMessage, self.enableForm ); } else { dao.addPet(pet, function (pet) { appendToTable(pet, humans); self.resetForm(); }, showErrorMessage, self.enableForm ); } return false; }); $('#btnClear').click(this.resetForm); }; this.getPetInForm = function () { var form = $(formQuery); return { 'id': form.find('input[name="id"]').val(), 'name': form.find('input[name="name"]').val(), 'type': form.find('input[name="type"]').val(), 'peopleID': document.getElementById('dueno').value }; }; this.getPetInRow = function (id) { var row = $('#person-' + id); if (row !== undefined) { return { 'id': id, 'name': row.find('td.name').text(), 'type': row.find('td.pet_type').text(), 'peopleID': row.find('td.surname').text() }; } else { return undefined; } }; this.editPet = function (id) { var row = $('#person-' + id); if (row !== undefined) { var form = $(formQuery); form.find('input[name="id"]').val(id); form.find('input[name="name"]').val(row.find('td.name').text()); form.find('input[name="surname"]').val(row.find('td.surname').text()); form.find('input[name="type"]').val(row.find('td.pet_type').text()); $('input#btnSubmit').val('Modificar'); } }; this.deletePet = function (id) { if (confirm('Está a punto de eliminar a una mascota. ¿Está seguro de que desea continuar?')) { dao.deletePet(id, function () { $('tr#person-' + id).remove(); }, showErrorMessage ); } }; this.isEditing = function () { return $(formQuery + ' input[name="id"]').val() != ""; }; this.disableForm = function () { $(formQuery + ' input').prop('disabled', true); }; this.enableForm = function () { $(formQuery + ' input').prop('disabled', false); }; this.resetForm = function () { $(formQuery)[0].reset(); $(formQuery + ' input[name="id"]').val(''); $('#btnSubmit').val('Crear'); }; } ; var returnHumansSelect = function (humans) { var toret = ""; return toret; }; var returnHumanTextBox = function (human) { var toret = ""; return toret; }; var insertPetForm = function (parent, humans, humans_map) { var txtToAppend = ""; if (peopleID === 'all') { txtToAppend = returnHumansSelect(humans); } else { txtToAppend = returnHumanTextBox(humans_map[peopleID]); } parent.append( '
' ); }; var insertPetList = function (parent) { parent.append( 'Nombre | \Dueño | \Tipo | \\ |
---|