gpt4 book ai didi

javascript - ExtJS 4.1.1 |为什么继承类的事件处理函数中 `view`和 `this`不同

转载 作者:行者123 更新时间:2023-11-28 02:42:34 25 4
gpt4 key购买 nike

我自己写的My.grid继承自Ext.grid.Panel并将监听器设置为 itemdblclick事件。

该事件的处理函数获取view作为第一个参数,有 this作为范围。

我不明白为什么我在 view 中得到不同的值和this

  • thisMy.grid 的一个实例

  • viewExt.grid.Panel 的一个实例(但我预计 My.grid )

可能是我做错了什么?或者这是 ExtJS 的错误/功能?

如何在那里编写继承的小部件view引用继承的对象?

这是一个简单的例子:

<!DOCTYPE html>
<html>
<head>
<title>ExtJS Test page</title>
<link rel="stylesheet" href="http://cdn.sencha.io/ext-4.1.1-gpl/resources/css/ext-all-gray.css">
<script type="text/javascript" charset="utf-8" src="http://cdn.sencha.io/ext-4.1.1-gpl/ext-all.js"></script>
<script type="text/javascript">
Ext.create('Ext.data.Store', {
storeId: 'simpsonsStore',
fields: ['name', 'email', 'change'],
data: {
'items': [
{ 'name': 'Lisa', 'email': 'lisa@simpsons.com', 'change': 100 },
{ 'name': 'Bart', 'email': 'bart@simpsons.com', 'change': -20 },
{ 'name': 'Homer', 'email': 'home@simpsons.com', 'change': 23 },
{ 'name': 'Marge', 'email': 'marge@simpsons.com', 'change': -11 }
]
},
proxy: { type: 'memory', reader: { type: 'json', root: 'items' } }
});

Ext.define('MY.grid', {
extend: 'Ext.grid.Panel',
alias: 'widget.simpsonsgrid',
title: 'Simpsons',
store: Ext.data.StoreManager.lookup('simpsonsStore'),
columns: [
{ header: 'Name', dataIndex: 'name' },
{ header: 'Email', dataIndex: 'email' },
{ header: 'Change', dataIndex: 'change' }
],
initComponent: function () {
this.callParent(arguments);
this.on('itemdblclick', this.test, this);
},
test: function (view, record) {
console.log(this); // instance of My.grid
console.log(view); // instance of Ext.grid.Panel
}
});

Ext.onReady(function () {
Ext.widget('simpsonsgrid', {
renderTo: Ext.getBody()
});
});
</script>
</head> <body></body> </html>

最佳答案

查看 API ( http://docs.sencha.com/ext-js/4-1/#!/api/Ext.grid.Panel )。您可以看到该特定事件具有以下签名:

itemdblclick(
Ext.view.View this,
Ext.data.Model record,
HTMLElement item,
Number index,
Ext.EventObject e,
Object eOpts
)

所以实际上你应该在 view 参数中获得 Ext.grid.View 的实例。您可以通过访问view.ownerCt来获取MY.grid的实例。

示例:http://jsfiddle.net/TLEFH/

关于javascript - ExtJS 4.1.1 |为什么继承类的事件处理函数中 `view`和 `this`不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12443899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com