Javascript ItemTpl onclick函数调用extjs 6
如何在dataview上调用onclick函数Javascript ItemTpl onclick函数调用extjs 6,javascript,extjs,sencha-touch,extjs6,Javascript,Extjs,Sencha Touch,Extjs6,如何在dataview上调用onclick函数 itemTpl: [ '<a onClick="this.getViewController().somefunction({id},{code});" href="#">{code}</a><br/>', ] 给出了 此.getViewController()不是函数 注意:我不想做MyApp.app.getController(),因为我有很多ViewController,我不能将所有逻辑都放在center
itemTpl: [
'<a onClick="this.getViewController().somefunction({id},{code});" href="#">{code}</a><br/>',
]
给出了
此.getViewController()不是函数
注意:我不想做
MyApp.app.getController()
,因为我有很多ViewController,我不能将所有逻辑都放在centeral控制器中如果你真的想在itemTpl
中绑定一个单击函数,你需要指定模板中可以访问的函数,如下所示:
// Save 'this' scope to me variable for use in XTemplate scope
var me = this;
// ...
itemTpl: [
'<a onClick="this.someFunction();" href="#">{code}</a><br/>',
// Defining funtions accessible in the XTemplate
{
someFunction: function() {
// Use 'me' here, because 'this' is XTemplate
me.getViewController().someFunction();
}
}
]
我通常在ExtJS 4中工作,这是从ExtJS 6中得到的,因此请原谅我犯的任何错误并纠正我,以便我可以编辑答案。单击侦听器的问题是,我无法传递参数,因为它们正在动态检查第一种方法…不起作用,看起来不错,这就是为什么我投了赞成票,但没有起作用,给出相同的错误this.somefunction不是函数您是说单击侦听器?在我的示例代码中,有关于不直接在侦听器中使用
this
的注释。你用替代变量了吗?你能为你的问题提供一个最小的工作代码吗?在第一个代码示例中,正确的代码是'
,…
@DevN不,不是<代码>me未在模板代码中定义,因此建议的代码将以异常结束。您使用的是ext6还是sencha touch?如果是前者,它是现代的还是经典的工具包?
// Save 'this' scope to me variable for use in XTemplate scope
var me = this;
// ...
itemTpl: [
'<a onClick="this.someFunction();" href="#">{code}</a><br/>',
// Defining funtions accessible in the XTemplate
{
someFunction: function() {
// Use 'me' here, because 'this' is XTemplate
me.getViewController().someFunction();
}
}
]
// Save 'this' scope to me variable for use in the item scope
var me = this;
// ...
itemConfig: {
listeners: {
click: function() {
// Use 'me' here, because 'this' is the item
me.getViewController().someFunction();
}
}
}