2013-01-22 1 views
0

제품보기 페이지에서 사용자 정의 JS 파일을 호출하는 Magento 확장을 만들고 있습니다. 이 사용자 정의 JS 파일은 마지막에로드되며 /js/varien/product.js의 맨 아래에있는 formatPrice() 함수를 재정의해야합니다. 다음과 같이js/varien/product.js의 JS 함수를 어떻게 재정의합니까?

원래 formatPrice 기능은 다음과 같습니다

formatPrice: function(price) { 
return formatCurrency(price, this.priceFormat); 
} 
내가/교체 다음과 같이이 함수를 재정의하고 싶은

:

formatPrice: function(price) { 
if (price % 1 == 0) { this.priceFormat.requiredPrecision = 0; } 

return formatCurrency(price, this.priceFormat); 
} 

을 내 사용자 정의 JS에서 JS 코드를 작성하려면 어떻게 파일이 제대로이 함수를 재정의 할 수 있도록? 나는 JS에 대해 충분히 잘 알고 있지 않다.

+2

'formatPrice'의 어떤 객체가 어떤 객체입니까? 그것은 세계적인가? – bfavaretto

+0

bfavaretto처럼 :'formatPrice'가 정의 된 위치에 따라 다릅니다. – Alp

+0

나는 그것이 Product.OptionsPrice.prototype 안에 있다고 믿는다. Product.OptionsPrice.prototype 선언 직전의'Product.OptionsPrice = Class.create();'행을 사용하여이 프로토 타입으로 객체를 만듭니다. – iakkam

답변

2

가 글로벌 인 경우는 다음과 같이 할 것 객체의 구성원 인 경우 다음 당신은 window.formatPrice = myNewFormatPrice;을 수행 할 수 있습니다 anObject.formatPrice = myNewFormatPrice;

하면 객체 사용의 프로토 타입을 편집해야하는 경우 : Product.OptionsPrice.prototype.formatPrice = myFormatPrice;

또한 requiredPrecision에 대한 액세스를 조사해야합니다. 그것이 "개인"이거나 "보호 된"경우 액세스 할 수 없습니다. @ jholloman의 대답은 기능적인 관점에서 올바른 동안

+0

좋아요, 알아 냈습니다 : function myFormatPrice (price) {if (price % 1 == 0) {this.priceFormat.requiredPrecision = 0; } 반환 formatCurrency (price, this.priceFormat); } Product.OptionsPrice.prototype.formatPrice = myFormatPrice;'도와 줘서 고마워. – iakkam

0

, 당신은 Product.OptionsPrice에서 상속 대신 그 새로운 클래스를 사용하여,이 프로토 타입의 방법을 수행하는 것이 좋습니다. 당신은

원래

<script type="text/javascript"> 
    var optionsPrice = new Product.OptionsPrice(<?php echo $this->getJsonConfig() ?>); 
</script> 

wrap() (이 방법을 사용하여

<script type="text/javascript"> 
    var MyOptionPrice = Class.create(Product.OptionsPrice, { // inherit from Product.OptionsPrice 
     formatPrice: function($super, price) { // $super references the original method (see link below) 
      if (price % 1 === 0) { 
       this.priceFormat.requiredPrecision = 0; 
      } 
      return $super(price); 
     }   
    }); 
    var optionsPrice = new MyOptionPrice(<?php echo $this->getJsonConfig() ?>); // use yours instead 
</script> 

수정 : 이것은, app\design\frontend\base\default\template\catalog\product\view.phtml에서 (당신이 그것을 변경해야 할 가정) 라인 36 원래 메소드 이름을 변경할 필요가 없음) :

<script type="text/javascript"> 
    Product.OptionsPrice.prototype.formatPrice = Product.OptionsPrice.prototype.formatPrice.wrap(function(parent, price) { 
     if (price % 1 === 0) { 
      this.priceFormat.requiredPrecision = 0; 
     } 
     return parent(price);   
    }); 
    var optionsPrice = new Product.OptionsPrice(<?php echo $this->getJsonConfig() ?>); 
</script> 

프로토 타입의 상속에 대해서는 this link이고 $ 수퍼 var를 참조하십시오.
Magento에서 사용 된 @ jholloman의 제안과 비슷한 코드를 보았으므로 아무런 문제가 없습니다.하지만이 프로토 타입의 방법을 알고 싶을 수도 있습니다.

+0

고맙습니다. 나는이 접근에 대한 이론적 근거를 이해할 수있다. – iakkam

관련 문제