如何在magento中覆盖product.js文件

Mah*_*man 9 magento

我想覆盖product.js文件,在一些函数中添加一些额外的值.我将文件从js/mage/adminhtml/product.js复制到我的文件夹,如js/myfolder/myproduct.js.How我可以使用我尝试的文件的功能,它不适合我.当我更改对象名称时,它将显示没有错误,但没有任何事情发生(Products.Gallery = Class.create()原始是Product.Gallery = Class.create();).任何人都可以告诉我如何使用myproduct的所有功能而不会与原始功能冲突.

谢谢

iva*_*dja 21

比方说,你要覆盖function loadImagejs/mage/adminhtml/product.js产品的编辑页面.

创建自定义js js/myfolder/myproduct.js::

Product.Gallery.addMethods({
    loadImage : function(file) {
        alert('hi there');
        var image = this.getImageByFile(file);
        this.getFileElement(file, 'cell-image img').src = image.url;
        this.getFileElement(file, 'cell-image img').show();
        this.getFileElement(file, 'cell-image .place-holder').hide();
    }
});
Run Code Online (Sandbox Code Playgroud)

参考:http://prototypejs.org/learn/class-inheritance.html

然后在你的布局xml中添加你的自定义js:

<adminhtml_catalog_product_edit>
    <reference name="head">
        <action method="addJs"><script>myfolder/myproduct.js</script></action>
    </reference>
</adminhtml_catalog_product_edit>
Run Code Online (Sandbox Code Playgroud)

使用此方法时,function loadImage只有在包含您的方法时才会覆盖此方法js/myfolder/myproduct.js.

PS:确保js/myfolder/myproduct.js包含在之后js/mage/adminhtml/product.js(虽然它是默认的,因为js/mage/adminhtml/product.js包含在<default>标签中)