Dojo:ComboBox不是构造函数

Dojo : ComboBox is not a constructor

本文关键字:构造函数 ComboBox Dojo      更新时间:2023-09-26

我正在编写一个使用dojo Toolkit的JavaScript应用程序。我在dojo文件夹中创建了一个名为"md"(My dojo)的文件夹。其中有两个.js文件myownclass.jsmyComboBox.jsmyownclass.js用于通过向sqlaccess.php发出dojo请求(工作)来从SQLite数据库检索信息。firebug告诉我这个请求是正确的。但是函数不想转到函数prepare_str(str)并将字符串推送到Array数据:[]。

数据:[],

constructor : function( ){
        this.getdata();
    },
    //gets Data
getdata : function(){
    xhr("sqlaccess.php?order=0").then(function(text){
                                                                                    this.prepare_str(text.toString().split(";"));});
        return this.data;
    },
    //push Data to Array    
prepare_str : function(str){
    alert(str);
    for(var index = 0; index < str.length; index++)
    {
        data.push({name:str[index], id: index});
    }
    //get
    },

您可以在中看到完整的代码:http://pastebin.com/aKUrLmcH

另一个错误是在myComboBox.js中,它告诉我新的ComboBox()不是构造函数

MyMemory : null,
        comboBox : null,
        //keyhandle : null,
        oldval : "",
        constructor: function(){
            this.MyMemory = new myownclass();
            var stateStore = new Memory({data : this.MyMemory.data});
            comboBox = new ComboBox({
                            id: "stateSelect",
                            name: "state",
                            value: "",
                            store: stateStore,
                        searchAttr: "name"
                        }, "stateSelect");
                var handle = on(dom.byId("stateSelect"),"keyup", this.keyupfoo);        
            },

完整代码可在此处阅读:http://pastebin.com/hi8nkymf

有人能看到我犯的错误吗?

感谢的帮助

prepare_str()函数未被调用的第一个问题是由于作用域问题。当您依赖回调时,this将不再引用您的类。要解决此问题,可以使用dojo/_base/lang模块的hitch()方法,例如:

xhr("sqlaccess.php?order=0").then(lang.hitch(this, function(text){
    this.prepare_str(text.toString().split(";"));
}));

这确保了xhr()函数的回调将使用当前对象作为作用域,因此this将引用您的类(包含prepare_str()函数)。


我不知道为什么你的组合框不起作用。确保模块加载正确,并注意在myComboBox的构造函数中不要忘记使用this

this.comboBox = new ComboBox({
    id: "stateSelect",
    name: "state",
    value: "",
    store: stateStore,
    searchAttr: "name"
}, "stateSelect");

如果你想在构造函数问题上得到进一步的帮助,你应该试着制作一个错误所在控制台的屏幕截图,或者至少给出错误的完整堆栈信息(我假设它位于new ComboBox()语句中,但我想确定一下。