Javascript:undefined不是一个函数

时间:2014-04-14 13:43:25

标签: extjs sencha-touch

这是我的控制器的代码:

Ext.define(controller.details.StudentControlller', {
    extend: 'Ext.app.Controller',
    requires: [
    ],
    config: {
},
Init: function(){
"use strict";
        var me = this;
        this.app = this.getApplication();
    this.createStudentTables();
}
createStudentTables: function () {
var finalStudent=[],
view=this.getStudentsTableView();

arrReqTplTest = ['<div class="StuReq"><table><tr><th class=" StuReq ">' +
                'NAME  ' +
                '<img src="resources/images/arrow-bottom-1-512.png" width="10px" height="10px" onclick="this.sortStore();"/>' +
                '</th><th class=" StuReq ">CATEGORY ' +
                ' <img src="resources/images/arrow-bottom-1-512.png" width="10px" height="10px" onclick="sortStore();"/>' +
                '</th><th class=" StuReq ">STUDENT GROUP  ' +
                '<img src="resources/images/arrow-bottom-1-512.png" width="10px" height="10px" onclick="sortStore();"/>' +                
</th></tr></table></div>'];
finalStudent.push({
title: “Student Table”,
                collapsed: true,
                layout: 'fit',
                items: [
                    {
                        xtype: 'component',
                        itemId: 'StudentTablViewID-' + i,
                        html: arrReqTplTest

                    },
                    {
                        xtype: 'dataview',
                        itemId: 'StudentTable-' + i,
                        height: 200,
                        store: ‘studentDetailStore’,
                        //itemHeight: 70,
                        scrollable: false,           
                        itemSelector: 'div.wrap-requirements-' + i
                    }
                ]
            });
        }

        view.add(finalStudent);
})
}
sortStore: function(){
        alert("Now it is finally undefined");
    }
});

在上面的代码中,您可以看到我已经为我在列标题旁边附加的图像编写了一个onclick。当我点击该图像时,它应该调用名为sortStore的函数,但它只是继续抛出名为&#34的错误;未捕获的TypeError:undefined不是函数&#34;

如果我做了一些语法错误,请原谅我,因为我缩短了代码以使其可读。但请放心,除了sortStore()调用之外的所有内容都可以正常工作。

2 个答案:

答案 0 :(得分:2)

查看这个小提琴https://fiddle.sencha.com/#fiddle/536

我将其创建为一个面板,并将事件监听器附加到img元素,如下所示。

Ext.define('test.view.SignupPanel', {
    extend: 'Ext.Panel',
    xtype: 'mypanel',
    config: {
      itemId: 'testItem',
      layout: 'fit',
      html:'<img  src="http://www.sencha.com/img/v2/logo.png"/>',
      listeners:{
        initialize:function(obj){ 
               var me= this;
               obj.element.down('img').on('tap', me.imageTap, this, me);
            }
      }
    },
    imageTap:function(obj){
        Ext.Msg.alert('Hi', this.xtype);
    }

});

答案 1 :(得分:0)

问题是您使用字符串绑定侦听器。只有在全局级别(窗口)上有函数sortStore时,才会触发此侦听器。这个监听器永远不会像你期望的那样在当前范围内执行。

推荐的方法是创建Ext.Img并将点击侦听器绑定到它。或者使用xtype: 'image'

Ext.create('Ext.Img', {
    src: 'resources/images/arrow-bottom-1-512.png',
    listener: {
        click: function(){ ... }
    }
});