Ver Mensaje Individual
  #1 (permalink)  
Antiguo 25/06/2014, 17:23
Avatar de Copia
Copia
 
Fecha de Ingreso: noviembre-2009
Mensajes: 309
Antigüedad: 14 años, 5 meses
Puntos: 4
Angularjs, no actualiza modelo

Buenas tardes, estoy trabajando con angularjs y estoy teniendo un error que no entiendo bien el porque sucede.

Desde el template cuyo controller es giftcardsCtrl, ejecuto la funcion addItemCard cuando quier agregar un item al carro de compra. Si en cambio quiero quitar un item ejecuto la funcion removeItemCard

Tanto removeItemCard como addItemCard tienen un proceso comun ( cartGiftCard, setDataCart ) que he tenido que separar porque al usar addItemCard creo que estoy perdiendo la referencia a $scope.

Luego tengo otro problema con totalCartZP y balanceCartZP que en setDataCart funcionan como espero(al modificar su valor, se muestra en el template) pero en cartGiftCard no( al usar el inspector muestra que si setea).

Código Javascript:
Ver original
  1. app.controller('giftcardsCtrl', function ($scope, $http, helper, onEvent, Modal, Param, Status)
  2. {
  3.    
  4.     $scope.card = {'name':'','prices':''};
  5.     $scope.queueCard = [];
  6.     $scope.totalCartZP = 0;
  7.     $scope.balanceCartZP = 0;
  8.  
  9.     $scope.cartGiftCard = function(cart){
  10.         if($scope.$parent.queueCard.length > 0){
  11.             $scope.$parent.queueCard.length = 0;
  12.         }
  13.         var total = 0;
  14.         angular.forEach(cart, function (value){
  15.             for(var i = 0; i < value.quantity; i++ ) {
  16.                 total = total + value.points;
  17.                 $scope.$parent.queueCard.push({
  18.                     'item' : value.item,
  19.                     'price' : value.price,
  20.                     'name' : value.name,
  21.                     'points' : value.points
  22.                 });
  23.             }
  24.             i = 0;
  25.         });
  26.         $scope.totalCartZP = total;
  27.         $scope.balanceCartZP =   $scope.totalCartZP - total;      
  28.     };
  29.  
  30.  
  31.     $scope.setDataCart = function(cart){
  32.         var total = 0;
  33.         if($scope.queueCard.length > 0){
  34.             $scope.queueCard.length = 0;
  35.         }
  36.         var total = 0;
  37.         angular.forEach(cart, function (value){
  38.             for(var i = 0; i < value.quantity; i++ ) {
  39.                 total = total + value.points;
  40.                 $scope.queueCard.push({
  41.                     'item' : value.item,
  42.                     'price' : value.price,
  43.                     'name' : value.name,
  44.                     'points' : value.points
  45.                 });
  46.             }
  47.             i = 0;
  48.         });
  49.         $scope.totalCartZP = total;
  50.         $scope.balanceCartZP =   $scope.totalCartZP - total;
  51.     };
  52.  
  53.     $scope.status.queue = false;
  54.  
  55.     $scope.addItemCard = function (){
  56.         var push = {
  57.             price       : $scope.getCard.price,
  58.             image       : $scope.getCard.image,
  59.             quantity    : $scope.getCard.quantity
  60.         };
  61.  
  62.         $http.post('giftcard/item/add', push).success(function (data, status)
  63.         {
  64.             if (data.success == true) {
  65.                 $scope.cartGiftCard(data.data.gifts);
  66.             } else {
  67.                 $scope.flash = {"message" : data.msg, "success": "warning"};
  68.             }
  69.         });
  70.     };
  71.  
  72.     $scope.removeItemCard = function (item){
  73.         $http.get('giftcard/item/remove/'+item).success(function (data, status) {
  74.             if (data.success == true)
  75.             {
  76.                 $scope.setDataCart(data.data.gifts);
  77.             }
  78.          });
  79.     };
  80.  
  81.    
  82. });

Que puede estar ocurriendo?
__________________
Dejando una huella de mi existencia por la red en http://tiricaya.com