本文目录一览:
extjs怎么自定义函数
extjs的自定义事件主要分三步
1.在类中定义事件名称:
Person = function(config) {
Person.superclass.constructor.call(this, config);
this.name = config.name || '';
this.sex = config.sex || '';
this.addEvents({
"nameChange": true,
"sexChange": true
});
};
2.实例化对象,并在该对象中定义事件的监听函数
var person = new Person({
name: 'binoruv',
sex: 'man'
});
person.on("nameChange", function() { alert("Name has been changed to " + this.name); });
person.on("sexChange", function() { alert("Sex has been changed to " + this.sex); });
3.触发事件
Ext.extend(Person, Ext.util.Observable, {
//extend函数中可以自定义新的方法,也可以重写原类的方法
setName: function(_name) {
if (this.name != _name) {
this.name = _name;
this.fireEvent("nameChange", this);
}
},
setSex: function(_sex) {
if (this.sex != _sex) {
this.sex = _sex;
this.fireEvent("sexChange", this);
}
}
});
person.setName("binoruv");
person.setSex("woman");
整个代码如下:
script type="text/javascript"
Person = function(config) {
/*
此处addEvents是调用Person父类Ext.util.Observable的方法
api中对Observable的描述:一个抽象基类(Abstract base class),为事件
机制的管理提供一个公共接口。子类应有一个"events"属性来
定义所有的事件。
*/
Person.superclass.constructor.call(this, config);
this.name = config.name || '';
this.sex = config.sex || '';
this.addEvents({
"nameChange": true,
"sexChange": true
});
};
Ext.extend(Person, Ext.util.Observable, {
setName : function(_name) {
if (this.name != _name) {
this.name = _name;
this.fireEvent("nameChange", this);
}
},
setSex : function(_sex) {
if (this.sex != _sex) {
this.sex = _sex;
this.fireEvent("sexChange", this);
}
}
});
Ext.onReady(function() {
var person = new Person({
name: 'binoruv',
sex: 'Man'
});
person.on("nameChange", function() { /*debugger;*/Ext.Msg.alert("Message","Name has been changed to " + this.name); });
person.on("sexChange", function() { /*debugger;*/Ext.Msg.alert("Message", "Sex has been changed to " + this.sex); });
person.setName("binoruv");
person.setSex("Woman");
});
/script
extjs函数调用方法
当前this所引用的作用域已经改变了,可简单修改为
init: function() {
var me = this;//保存作用域
this.control({
'userlist textfield[id=searchId]':{
keyup:function(textfield, e){
if (e.getKey() == 13) {
me .searchUserByEnter(textfield); //调用原作用域方法
}
}
}
}
规范点可参照extjs写成scope参数控制this
ExtJs里页面初始化函数是什么?
Ext.onReady();
参数:
1、初始化后执行的函数
(必须)
2、函数作用域
(可选)
3、选项【delay,single,buffer】
(可选)
举例:
Ext.onReady(function(){
console.log("Ext已加载");
}, this, {delay: 1000});