일부 고객의 불만과 마케팅 담당자와의 토론의 마지막 날에 구성 가능한 제품 옵션의 기본 동작을 변경하라는 요청이 있습니다. 고객/방문자를 혼란스럽게하고 가격 변경을 표시하지 않고 사용 가능한 옵션을 그대로 두었 기 때문에 옵션 드롭 다운에서 + $ xx.xx를 제거하도록 요청했습니다. 그들의 관점에서 볼 때 충분히 공정하지만, 개발자 입장에서 볼 때 다소 까다 롭습니다. 사이트에서 Magento CE 1.6.2를 실행 중이며 재정의/변경해야 할 파일은 /public_html/js/varien/configurable.js입니다. 가격 변경을 표시하지 않도록 getOptionLabel 함수를 변경해야합니다. 내 질문은 :이 파일을 변경하고 핵심 자바 스크립트 파일을 만지지 올바른 Magento 방법은 무엇입니까? 미리 감사드립니다. Magento 핵심 자바 스크립트 파일 재정의/확장
답변
여기에 어떤 객체 메소드 포장, 심지어 전화 "부모"필요한 경우 할 수 있습니다 프로토 타입 설명서 http://prototypejs.org/doc/latest/language/Function/prototype/wrap/에서이 참조하는 것은 의사의 샘플입니다 :
//where Product.Config is the object/class you need to "override"
Product.Config.prototype.getOptionLabel = Product.Config.prototype.getOptionLabel.wrap(function(parentMethod){
//replace the original method here with your own stuff
//or call parentMethod(); if conditions don't match
});
힌트에 대해 Anton에게 감사드립니다. 나는 아침에 그걸 가지고 놀고 그것이 어떻게 수행되는지 보게 될 것이다. –
Magento 1.7의 경우 다음과 같이 작동합니다. Product.Config.prototype.getOptionLabel = Product.Config.prototype.getOptionLabel.wrap –
메서드 이름이 어떻게 달라지는 지 추측 –
그냥 @의 안톤-S의 절대적 정답에 추가 할 수 또한 할 수있다 "전체"클래스는 재 작성 : 프로토 타입의 클래스가 아니라 이미 인스턴스화 된 객체에 대한 모든에만 작동합니다 이후
// Create the original class
var ClassA = Class.create();
ClassA.prototype = {
initialize: function(config) {
this.config = config;
},
test: function(msg) {
console.log('Hi from class A with message ' + msg);
}
};
// Create new class extending the original class
var ClassB = Class.create(ClassA, {
// $super is a reference to the original method
test: function($super, msg) {
console.log('Hi from class B');
console.log('this.config is accessible in class B: ' + this.config);
$super(msg + ' ...')
}
});
// To make the extend an override, you can do this:
ClassA = ClassB;
// ClassA now is ClassB overriding the original ClassA
var a = new ClassA('some config data');
a.test('Call A 1');
, 나는 또한 (어떤 랩 꽤 많이이 해킹에 던질거야) 않습니다 , too마음에
// Overriding a method of an already instantiated object
// There are many ways to do this more elegantly thanks to the amazing JS scoping magic
a.origTest = a.test;
a.test = function(msg) {
console.log('Hi from the patched method');
this.origTest(msg);
}
a.test('Call A 2');
유지 비록 wrap()
방법은 좋네요, 또한 클래스 정의에 콘크리트 인스턴스에 사용될 수있다.
// Wrap method of concrete instance
spConfig.getOptionLabel = spConfig.getOptionLabel.wrap(function(parentMethod, option, price) {
return parentMethod(option, price);
});
// Wrap method of class declaration
Product.Config.prototype.getOptionLabel = Product.Config.prototype.getOptionLabel.wrap(function(parentMethod, option, price) {
return parentMethod(option, price);
});
감사합니다. 귀중한 힌트로 Vinai와 Anton S에게 감사드립니다. 우리는 일시적인 js 해킹을 변경했으며 지금은 올바른 Magento 방식으로 수행됩니다. 너는 바위 야! –
@MilenPetrov는 비슷한 것을 검색하는 다른 사람들을 위해 SO 경험을 향상시키는 데 도움이되는 대답을 수락합니다. –
@Anton S - 정말 그 일을 잊어 버려서 죄송합니다. –
javascript는 무시합니다. 하지만 http://stackoverflow.com/questions/5409428/how-to-override-a-javascript-function – Guerra
@Guerra가 확실 프로토 타입 것을 볼은 IMO이이 친절하게) ( –
물건을 포장 할 수있는 방법이있다 OOP 오버라이드를 시뮬레이트하기위한 해킹은 괜찮지 만 작업은 괜찮습니다. – Guerra