2010-09-21 33 views
1

當save()執行this.reset()或that.reset()時,它找不到reset()方法並說它不是函數。我用的init()的解決方法,得到它的工作,但這種方法並沒有節省工作()變量作用域幫助:this.reset()不是函數

var vehicle = function() { 
    return { 
     init: function() { 
      var that = this; 

      jQuery('.vehicle-year-profile .options .delete').bind('click', function (e) { 
       e.preventDefault(); 
       that.remove(jQuery(e.currentTarget).parents('.vehicle-year-profile')); 
      }); 

      jQuery('.vehicle-year-profile .options .edit').bind('click', function (e) { 
       e.preventDefault(); 
       that.edit(jQuery(e.currentTarget).parents('.vehicle-year-profile').attr('id')); 
      }); 

      jQuery('#association-detail .save').bind('click', function (e) { 
       e.preventDefault(); 
       that.save(); 
      }); 
     }, 
     save: function() { 
      var data = new Array(); 
      data['onSet'] = ''; 
      var onSet = jQuery('#association-detail input:checked'); 
      for (var i = 0; i < (onSet.length-1); i++) { 
       data['onSet'] = data['onSet']+','+onSet.attr('id'); 
      } 

      var priceSet = jQuery('#association-detail input[type=text]'); 
      for (var i = 0; i < (priceSet.length-1); i++) { 
       data['priceSet'] = data['priceSet']+','+priceSet.attr('id')+':'+priceSet.val(); 
      } 

      jQuery.ajax({ 
       type: 'post', 
       url: '/ajax/store/product/saveAssocDetail.php', 
       data: data, 
        success: function (r) { 
        if (r.length > 0) { 
         document.triggerNotification('check', 'Changes have been saved'); 
         var that = this; 
         that.reset(); //ERROR IS TRIGGERED HERE 
        } else { 
         document.triggerNotification('x', 'Unable to save changes'); 
        } 
        }, 
       error: function() { 
        document.triggerNotification('x', 'Unable to process your request, ajax file not found'); 
        return false; 
       } 
      }); 
     }, 
     reset: function() { 
      jQuery('#association-detail h3').html(''); 
      jQuery('#assocationVehicleId').val(''); 

      jQuery('#association-detail input:checked').attr('checked', false); 
      jQuery('#association-detail input[type=text]').val('0.00'); 

      jQuery('#association-detail').hide(); 
     } 
    } 
}(); 
jQuery(function() { 
    vehicle.init(); 
}); 
+0

在我看來,你還沒有理解範圍(/上下文)如何在JavaScript中工作。 – adamse 2010-09-21 19:46:10

回答

6

也許是因爲你做出參考this你的Ajax調用內。嘗試把這一行:

var that = this; 

你讓你的Ajax調用之前,再明確提及that在你的Ajax調用。所以,像這樣:

var that = this; 

jQuery.ajax({ 
    type: 'post', 
    url: '/ajax/store/product/saveAssocDetail.php', 
    data: data, 
    success: function (r) { 
     if (r.length > 0) { 
      document.triggerNotification('check', 'Changes have been saved'); 

      /** 
      * now you can refer to "that", which is in the proper scope 
      */ 

      that.reset(); //ERROR IS TRIGGERED HERE 
     } else { 
      document.triggerNotification('x', 'Unable to save changes'); 
     } 
    }, 
    error: function() { 
     document.triggerNotification('x', 'Unable to process your request, ajax file not found'); 
     return false; 
    } 
});