Skip to content

Instantly share code, notes, and snippets.

@cristiandley
Last active August 29, 2015 14:01
Show Gist options
  • Save cristiandley/b38664df8bb4b33d8e27 to your computer and use it in GitHub Desktop.
Save cristiandley/b38664df8bb4b33d8e27 to your computer and use it in GitHub Desktop.
definicionABM
if(Meteor.isClient) {
/*
|---------------------------------------
| DEFINICION DE TEMPLATES
|---------------------------------------
*/
var Definicion = {
TemplateBase : Template.listadoPersonas,
TemplateAccion : Template.accionPersonas,
TemplateFormulario : Template.formularioPersonas,
Entidad : "Personas",
Collection : Personas,
Grupo : "personas",
camposReactiveTable:
[{
key: 'nombre',
label: 'Nombre',
}, {
key: 'detalles',
label: 'Detalles',
}, {
label: 'Acción',
tmpl: Definicion.TemplateAccion
}],
camposColeccion:
[{
key: 'nombre'
},
{
key: 'detalles'
},
{
key: 'habilitado'
},
{
key: 'fechaBaja'
}
],
crud:
{
create:{
titulo: "Nueva Persona",
error: "Error al cargar la Persona!",
ok: "Se cargo exitosamente la persona!"
},
read:{
titulo: "Ver Persona",
error: "Error al intentar ver la persona"
},
update:{
titulo: "Actualizar datos Persona",
error: "Error al actualizar los Datos!",
ok: "Persona actualizada!"
},
delete:{
titulo: "Eliminar Persona",
error: "Error al eliminar la fila!",
ok: "Persona eliminada!"
},
enable:{
error: "Error al habilitar Persona!",
ok: "Persona habilitada"
},
disable:{
error: "Error al deshabilitar Persona!",
ok: "Persona deshabilitada!"
}
}
};
/*
| ======================================================================================
| EDITAR SOLO SABIENDO QUE SE HACE !!!!!!!!!!
| ======================================================================================
*/
/*
|
| DEFINICION COLECCION
|
*/
var Col = Definicion.collection;
var BuscarDatos = function(datos){
if(datos != null){
return Col.find(datos);
}else{
return Col.find();
}
};
var InsertDatos = function(datos){
return Col.insert(datos);
};
var ActualizarDatos = function(id,datos){
return Col.update(id,datos);
};
var RemoverDatos = function(id){
return Col.remove(id);
};
/*
|
| Reseteo modal Form
|
*/
var resetModalForm = function(es){
var dato = Definicion.camposColeccion;
for(var i in dato)
{
$(es.modalTarget).find('[name='+dato[i]["key"]+']').val("")
}
};
/*
|
| Genero Query con los datos de los inputs
|
*/
var generarQueryDb = function (){
var dato = Definicion.camposColeccion;
var valJson = new Object();
for(var i in dato)
{
var val = $(es.modalTarget).find('[name='+datos[i]["key"]+']').val();
valJson[dato[i]["key"]] = (val == null)? false : val;
}
return valJson;
};
/*
| COLECCION PARA REACTIVE TABLE
*/
Definicion.TemplateBase.coleccionTabla = function() {
return BuscarDatos();
};
/*
| PROPIEDADES DE LA TABLA
*/
Definicion.TemplateBase.tableSettings = function() {
return {
rowsPerPage: 20,
/*
|
| DEFINICION COLUMNAS TABLA
|
*/
fields: Definicion.fieldsReactiveTable,
group: Definicion.TemplateAccion,
useFontAwesome: false
};
};
/*
|
| EVENTOS
|
*/
Definicion.TemplateBase.events({
/*
|
| NUEVO SOCIO
|
*/
'click #agregarFila': function(event) {
var create = {
template: Definicion.TemplateFormulario,
title: Definicion.crud.create.titulo,
buttons: {
"cancel": {
class: 'btn btn-default',
label: 'Cancel'
},
"ok": {
closeModalOnClick: false,
class: 'btn btn-primary cargarSocioBtn',
label: 'Guardar'
}
}
}
var es = ReactiveModal.initDialog(create);
resetModalForm(es);
/*
|
| BUTTON SUMBIT FOMULARIO
|
*/
es.buttons.ok.on('click', function(button) {
var addDocument = generarQueryDb();
Col.insert(addDocument, function(error, result) {
if(error) {
alertify.error(Definicion.crud.create.error);
console.log(error.reason)
};
if(result) {
alertify.success(Definicion.crud.create.ok)
};
});
});
es.show();
},
/*
|
| ELIMINAR FILA
|
*/
'click .reactive-table #eliminarFila': function(event) {
fila = this;
Col.remove(fila._id);
alertify.error(Definicion.crud.delete.ok);
},
/*
|
| EDITAR FILA
|
*/
'click .reactive-table #editarFila': function(event) {
var editRow = this;
Definicion.TemplateFormulario.socio = function() {
return editRow;
};
var editCollection = {
template: Definicion.TemplateFormulario,
title: Definicion.crud.update.titulo,
buttons: {
"cancel": {
class: 'btn btn-default',
label: 'Cancel'
},
"ok": {
closeModalOnClick: false,
class: 'btn btn-primary',
label: 'Ok'
}
}
}
var es = ReactiveModal.initDialog(editCollection);
/*
|
| SUBMIT EDITAR
|
*/
es.buttons.ok.on('click', function(button) {
console.log(es);
var updateRow = generarQueryDb();
Col.update(editRow._id, {"$set" : updateRow}, function(error, result) {
if(error) {
alertify.error(Definicion.crud.update.error);
console.log(error.reason)
};
if(result) {
alertify.success(Definicion.crud.update.ok)
};
});
});
es.show();
},
/*
| ======================================================================================
| OPCIONES AGREGADAS !!!!!!!!!!
| ======================================================================================
*/
/*
|
| DESHABILITAR FILA
|
*/
'click .reactive-table #deshabilitarFila': function(event) {
Col.update(this._id, {"$set" : {"habilitado": false}}, function(error, result) {
if(error) {
alertify.error(Definicion.crud.disable.error);
console.log(error.reason)
};
if(result) {
alertify.error(Definicion.crud.disable.ok)
};
});
},
/*
|
| HABILITAR FILA
|
*/
'click .reactive-table #habilitarFila': function(event) {
Col.update(this._id, {"$set" : {"habilitado": true}}, function(error, result) {
if(error) {
alertify.error("Notificacion: Error al habilitar un socio!");
console.log(error.reason)
};
if(result) {
alertify.success("Notificacion: Socio habilitado!")
};
});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment