|
|
|
|
//Funtions
|
|
function onSubmitAnimal(form){
|
|
var data = {};
|
|
$.map($(form).serializeArray(), function(n, i){
|
|
if(n['value']!= "") data[n['name']] = n['value'];
|
|
else data[n['name']] = null;
|
|
});
|
|
|
|
if(data._id){
|
|
$.ajax({
|
|
type: "PUT",
|
|
url: "/animal/"+data._id,
|
|
data: JSON.stringify(data),
|
|
success: function() { $("#animals").click() },
|
|
contentType : "application/json"
|
|
});
|
|
}else{
|
|
$.ajax({
|
|
type: "POST",
|
|
url: "/animal",
|
|
data: JSON.stringify(data),
|
|
success: function() { $("#animals").click() },
|
|
contentType : "application/json"
|
|
});
|
|
}
|
|
return false;
|
|
};
|
|
|
|
function editAnimal(id){
|
|
$( "#body" ).load( "/templates/animal-form.html", function(){
|
|
var animal;
|
|
$.get( "/animal/"+id, function( data ) {
|
|
animal = data.data
|
|
for(key in animal)
|
|
{
|
|
if(animal.hasOwnProperty(key)){
|
|
if(key == "father" || key == "mother" || key == "specie" || key == "sex"){
|
|
let a = key;
|
|
setTimeout(function(){
|
|
$('#a-'+a+' option[value='+data.data[0][a]+']').attr('selected','selected');
|
|
}, 200)
|
|
}else{
|
|
$('input[name='+key+']').val( data.data[0][key] ? data.data[0][key].$numberDecimal ? data.data[0][key].$numberDecimal : data.data[0][key] : "");
|
|
}
|
|
}
|
|
}
|
|
|
|
$.get( "/species", function( data ) {
|
|
data.data.map(function(item){
|
|
$("#a-specie").append("<option value='"+item._id+"'>"+item.name+"</option>");
|
|
})
|
|
});
|
|
paint_male_female(animal.specie, animal._id);
|
|
|
|
$("#a-specie").change(function(e){
|
|
paint_male_female($( this ).val(), animal._id);
|
|
})
|
|
});
|
|
});
|
|
}
|
|
|
|
function showAnimal(id){
|
|
$( "#body" ).load( "/templates/animal-show.html", function(){
|
|
var text = "<table><tr>";
|
|
$.get( "/animal/"+ id, function( data ) {
|
|
var animal = data.data,
|
|
j = 1;
|
|
$.each(animal, function(i, val) {
|
|
text += "<td>"+i+"</td><td>"+(val ? val.$numberDecimal ? val.$numberDecimal : val : "")+"</td>";
|
|
if(j % 3 == 0) text += "</tr><tr>"
|
|
j++;
|
|
});
|
|
text += "</tr></table>";
|
|
$("#a-name").html( );
|
|
$("#data").append( text );
|
|
// console.log(11)
|
|
get_parrents(animal);
|
|
// console.log(22)
|
|
get_childrens(animal);
|
|
// console.log(33)
|
|
});
|
|
|
|
});
|
|
}
|
|
|
|
function removeAnimal(id){
|
|
if(confirm("¿Surely you want to erase this dino?")){
|
|
$.ajax({
|
|
url: '/animal/'+id,
|
|
type: 'DELETE',
|
|
success: function() { $("#animals").click() }
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
function paint_male_female(specie, id){
|
|
$(".option").remove();
|
|
$.get( "/animal/"+specie+"/Male", function( data ) {
|
|
data.data.map(function(item){
|
|
if(item._id != id) $("#a-father").append("<option class=\"option\" value='"+item._id+"'>"+item.name+" - "+ item.level +"</option>");
|
|
})
|
|
});
|
|
|
|
$.get( "/animal/"+specie+"/Female", function( data ) {
|
|
data.data.map(function(item){
|
|
if(item._id != id) $("#a-mother").append("<option class=\"option\" value='"+item._id+"'>"+item.name+" - "+ item.level +"</option>");
|
|
})
|
|
});
|
|
}
|
|
|
|
function get_parrents(animal){
|
|
$.get( "/animal/parrents/" + animal._id, function( data ) {
|
|
data.data.map(function(item){
|
|
console.log(item);
|
|
});
|
|
});
|
|
}
|
|
|
|
function get_childrens(animal){
|
|
$.get( "/animal/childrens/" + animal._id, function( data ) {
|
|
data.data.map(function(item){
|
|
console.log(item)
|
|
})
|
|
});
|
|
}
|