1

saben ¿como evitar que hagan doble clic al enviar datos en un ajax?, osea click para enviar los datos y mientras se envian no dejar que vuelve dar otra vez click, puse un loading pero eso no es suficiente. Muchas gracias

.controller('productoGctrl', ['$scope', '$timeout', '$http','$ionicPopup', '$ionicLoading', 'productosDato','urlRoot', controlfproductoG])

function ontrolfproductoG ($scope, $timeout, $http, $ionicPopup, $ionicLoading, productosDato, urlRoot){
  $scope.agregarCanastika = function (id, tipo, nombre, preguntas, restantes) {
    $scope.divSepa = 'active';
    $scope.divAnimar = 'carritoAnimar';
    $scope.diviconoCar = 'active';

    $timeout(function () {
      $scope.divSepa = '';
      $scope.divAnimar = '';
      $scope.diviconoCar = '';
      var cantidadPreguntas = preguntas.length;
      var respondioP = respondioPreguntasProducto(id, tipo, cantidadPreguntas);
      console.log(id + '-' + tipo + '-' + restantes);
      if (id == undefined || id == '') {
       popupN('ooooppppsss', 'Id de proucto no disponible', 'advertencia', $ionicPopup);
      }
      else if (tipo == undefined || tipo == '') {
       popupN('ooooppppsss', 'Tipo de producto no disponible', 'advertencia', $ionicPopup);
      }
      else if (parseInt(restantes) <= 0) {
        popupN('Alerta', 'EL producto se ha agotado', 'advertencia', $ionicPopup);
      }
      else if (respondioP) {
        console.log(respondioP);
        var datoService = {
         actionType: 'get',
         iduser: localStorage.getItem('iduser'),
         idproducto: id
        };
        perguntasBuscarProducto($scope, $http, $ionicPopup, $ionicLoading, urlRoot.urlAjax + respondioP.tipoService,datoService)
         .then(function (date) {
           console.log(date);
           if (!date) {
             ajaxhttp(
               $http,
               $ionicLoading,
               $ionicPopup,
               'POST',
               urlRoot.urlAjax + 'CanastikaService.php',
               {
                actionType: 'agregar',
                iduser: localStorage.getItem('iduser'),
                tipo: tipo,
                idproducto: id
               },
               resAgregarCan($ionicLoading, $ionicPopup, urlRoot, nombre, 3)
            );
           }
           else{
            productosDato.nuevoProducto(id, tipo);
            direcionarGn('#/app/yagans');
           }
         });
      }
      else{
       console.log('noRespondio')
       $scope.show($ionicLoading);
       ajaxhttp(
         $http,
         $ionicLoading,
         $ionicPopup,
         'POST',
         urlRoot.urlAjax + 'CanastikaService.php',
         {
          actionType: 'agregar',
          iduser: localStorage.getItem('iduser'),
          tipo: tipo,
          idproducto: id
         },
         resAgregarCan($ionicLoading,$ionicPopup,urlRoot, nombre, 1)
       );
      }
    }, 500)
  }
}


function ajaxhttp($http,$ionicLoading,$ionicPopup,method,url,data,funrespuesta) {
  $http({
   method: method,
   url: url,
   headers: {'Content-Type': 'application/x-www-form-urlencoded'},
   data:data,
   timeout : 180000
  }).then(function (res) {
    funrespuesta(res.data)
  }, function (res) {
    $ionicLoading.hide();
    console.log(res);
    if (res.status == -1) {
      //popupC('ooooppppsss: '+ res.status, 'Se pasó el tiempo de plazo buscando la información','location', $ionicPopup);
      console.log('ooooppppsss: '+ res.status + 'Se pasó el tiempo de plazo buscando la información');
    }
    else{
     popupN('ooooppppsss: '+ res.status, res.statusText, 'advertencia', $ionicPopup);
    }
  });
}

function perguntasBuscarProducto ($scope, $http, $ionicPopup, $ionicLoading, url, dato) {
 var promise = new Promise();
 var preguntas;

 $scope.show = function() {
   $ionicLoading.show({
    template: '<p>Loading...</p><ion-diseno></ion-diseno>'
   });
 };

 $scope.hide = function(){
   $ionicLoading.hide();
 };

 $scope.show($ionicLoading);

 ajaxhttp(
  $http,
  $ionicLoading,
  $ionicPopup,
  'POST',
  url,
  dato,
  function (res) {
    console.log(res);
    $ionicLoading.hide();
    if (res.code == '0') {
      if (res.preguntas.length > 0) {
        promise.done(res.preguntas)
      }
      else{
        promise.done(false);
      }
    }
    else{
     console.log(res)
    }
  }
 );
 return promise;
}
3
  • puedes agregar tu codigo?
    – sioesi
    el 19 dic. 2016 a las 19:39
  • @sioesi ya lo puse, edité la pregunta el 19 dic. 2016 a las 19:51
  • puedes colocar una variable isclick=true; y validar... cuando comienze la ejecucion cambiar a false; y volver a cambiar a true al terminar
    – Bryro
    el 28 ago. 2019 a las 15:18

1 respuesta 1

2

Veo que utilizas ionic, por lo tanto esto te puede servir :

Veo que defines funciones y pasas por parametro todas las inyecciones de dependencia que tiene tu controlador. No veo muy bueno eso SI es que ocupas esas funciones desde otros controladores, aunque veo que las defines dentro de tu controlador es por eso que te aconsejo que las declares como las tienes o bien con algun $scope.

Dentro de tu controlador defines :

$scope.show = function() {
    $ionicLoading.show({
        template: '<ion-spinner icon="android"/>'
    });
};
$scope.hide = function() {
    $ionicLoading.hide();
};

Y cuando hagas el llamado a tu funcion ajax puedes hacer esto :

$scope.ajaxhttp = function(method,url,data,funrespuesta) {
    $scope.show();
    $http({
        method: method,
        url: url,
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        data: data,
        timeout: 180000

    }).then(function(response) {
        $scope.hide();
    }, function error(response) {
        /* Error llamada */
        $scope.hide();
    });
}
3
  • Si es por esa función ajaxhttp y perguntasBuscarProducto las utilizo en varios conotroladores diferentes. el 19 dic. 2016 a las 20:06
  • si de la misma manera como lo hice, la funcion ajaxhttp cambian de parametros y de perguntasBuscarProducto son los mismos parametros, los envie asi porque el ionicPopup cambia de alerta y el de ionicloading es para mostrar y si termina se colca el hide. el 19 dic. 2016 a las 20:11
  • 1
    ahhh si eso hacia, pero hasta ahora noté que me falta en if else colcarle el ionicLoading, muchas gracias, tu respuesta me sirve también :) el 19 dic. 2016 a las 20:15

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.