2012-11-14 156 views
9

我想重写product.js文件以在某些函数中添加一些额外的值。我已将文件从js/mage/adminhtml/product.js复制到我的文件夹,如js /myfolder/myproduct.js.How我可以使用该文件的功能,因为我尝试和它不为我工作。当我更改对象名称比它将显示没有错误,但没有任何情况发生原始是Product.Gallery = Class。创建();)。 任何人都可以告诉我如何使用myproduct的所有功能,而不会与原始功能发生冲突。如何覆盖magento中的product.js文件

感谢

回答

20

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

创建自定义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(); 
    } 
}); 

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

然后在你的布局文件添加自定义的JS:

<adminhtml_catalog_product_edit> 
    <reference name="head"> 
     <action method="addJs"><script>myfolder/myproduct.js</script></action> 
    </reference> 
</adminhtml_catalog_product_edit> 

使用这种方法,function loadImage会只有包含您的js/myfolder/myproduct.js才会被覆盖。

PS: 确保js/myfolder/myproduct.jsjs/mage/adminhtml/product.js后包括(尽管它就是这样的,因为默认情况下js/mage/adminhtml/product.js中包括<default>标签)

+0

嗨ivantedja谢谢您的回答。我做了根据你的指令和它的工作为上述功能,但当我尝试重写一些其他功能,如 updateImages:function(file){} 它显示我this.getFileElement(文件,“单元格标签输入”)是undefined.in上面的代码我添加了两个额外的字段与名称链接和target_window和这个代码工作在核心文件,但不是在我的自定义JS? –

+0

嗨,我只通过重命名完成并将js文件直接包含在我的phtml中,比如 它的工作原理。 –

+1

你在哪个页面重写它?和哪个功能? updateImage或updateImages? – ivantedja

相关问题