要在后端监听对 Magento 选项卡 ( varienTabs
) 的点击,您只需将自定义观察者添加到现有观察者。使用您的示例(后端中的“编辑客户页面”),这将是:
var myTabs = $$("#customer_info_tabs li a.tab-item-link");
for (var i = 0; i < myTabs.length; i++) {
Event.observe(myTabs[i], "click", function (event) {
var tab = Event.findElement(event, "a");
// insert your custom code here
alert(tab.id);
Event.stop(event);
});
}
要在不更改核心文件(破坏可升级性)的情况下实现自定义观察者,您可以覆盖适当的管理控制器操作。
例如,覆盖Mage_Adminhtml_CustomerController::editAction()
:
<!-- app/code/local/My/Adminhtml/etc/config.xml -->
<config>
<modules>
<My_Adminhtml>
<version>0.1.0</version>
</My_Adminhtml>
</modules>
<admin>
<routers>
<adminhtml>
<args>
<modules>
<My_Adminhtml before="Mage_Adminhtml">My_Adminhtml</My_Adminhtml>
</modules>
</args>
</adminhtml>
</routers>
</admin>
</config>
接下来,定义您的自定义管理控制器:
// app/code/local/My/Adminhtml/controllers/CustomerController.php
require 'Mage/Adminhtml/controllers/CustomerController.php';
class My_Adminhtml_CustomerController extends Mage_Adminhtml_CustomerController
{
public editAction()
{
// copy of Mage_Adminhtml_CustomerController::editAction() code here
}
}
最后,在被覆盖的操作中,创建一个包含自定义观察者脚本的附加文本块,并将该块附加到布局中。例如在editAction
插入的末尾是这样的:
:
$this->loadLayout();
$oBlock = $this->getLayout()->createBlock('core/text')->setText('
<script type="text/javascript">
var myTabs = $$("#customer_info_tabs li a.tab-item-link");
for (var i = 0; i < myTabs.length; i++) {
Event.observe(myTabs[i], "click", function (event) {
var tab = Event.findElement(event, "a");
alert(tab.id);
Event.stop(event);
});
}
</script>
');
$this->getLayout()->getBlock('left')->append($oBlock);
$this->renderLayout();
: