Hierarchy
Ext.BaseExt.data.IdGeneratorSubclasses
Files
本类是所有id生成器的基类. 本类还提供通过生成器ID来查找id生成器.
通常情况下, 一个id生成器是用来为一个新数据实例生成主键. 关于这个问题有几种不同的解决办法, 所有此处提供 两个简单用例, 并且易于自定义扩展. 一个Model可以用Ext.data.Model.idgen 属性来配置ID生成器.
使用共享ID生成器来保证配置的唯一或公用, 通常来说是个很好的选择. 实现方法即为向ID生成器提供id属性, 以使 其能够通过get方法来获取. 例如要配置两个数据模型, 让他们共用同一个 sequential(序列)ID生成器, 你可以像这样用同一id关联他们:
Ext.define('MyApp.data.MyModelA', {
extend: 'Ext.data.Model',
idgen: {
type: 'sequential',
id: 'foo'
}
});
Ext.define('MyApp.data.MyModelB', {
extend: 'Ext.data.Model',
idgen: {
type: 'sequential',
id: 'foo'
}
});
为了使生成器类型尽可能简单的被非常多(或全部)数据模型公用, ID生成器的类型(如'sequential','uuid')也 同样作为了生成器的保留id. 像使用Ext.data.UuidGenerator时, 其id就等于其类型('uuid'). 换句话说, 可以像下面这样实现多模型共享生成器:
Ext.define('MyApp.data.MyModelX', {
extend: 'Ext.data.Model',
idgen: 'uuid'
});
Ext.define('MyApp.data.MyModelY', {
extend: 'Ext.data.Model',
idgen: 'uuid'
});
虽然这是可以被重写的(通过明确声明id), 但是对于这个生成器类型, 通常没有什么特别好的理由去这么干.
一个ID生成器应该继承自本类, 并实现generate方法. 由于构造函数中已经将所有参数赋值到新实例 中, 所以通常不需要覆盖构造函数.
要注册一个ID生成器的类型, 扩展类需要提供'alias'属性, 就像这样:
Ext.define('MyApp.data.CustomIdGenerator', {
extend: 'Ext.data.IdGenerator',
alias: 'idgen.custom',
configProp: 42, // 一些配置参数及默认值
generate: function () {
return ... // 返回一个新id
}
});
然后就可以非常简便的使用这个id生成器:
Ext.define('MyApp.data.MyModel', {
extend: 'Ext.data.Model',
idgen: 'custom'
});
// 或者...
Ext.define('MyApp.data.MyModel', {
extend: 'Ext.data.Model',
idgen: {
type: 'custom',
configProp: value
}
});
不推荐对公用ID生成器进行参数配置, 因为这需要所有模型的参数配置都保持一致, 否则可能会导致无法预知的 结果(而且很冗余). 这时使用一个带默认id的自定义生成器类是最好的解决方案.
Ext.define('MyApp.data.CustomIdGenerator', {
extend: 'Ext.data.SequentialIdGenerator',
alias: 'idgen.custom',
id: 'custom', // 默认共享
prefix: 'ID_',
seed: 1000
});
Ext.define('MyApp.data.MyModelX', {
extend: 'Ext.data.Model',
idgen: 'custom'
});
Ext.define('MyApp.data.MyModelY', {
extend: 'Ext.data.Model',
idgen: 'custom'
});
// 上面几个模型共享的ID生成器会生成 ID_1000, ID_1001, ...
扩展事件
Defaults to: []
用'true'来标识一个对象是一个ID生成器(IdGenerator)类或其子类的实例.
Defaults to: true
本身
获取当前类的引用,此对象被实例化。不同于 statics,
this.self
是依赖范围,它意味着要使用动态继承。
参见 statics 详细对比
Ext.define('My.Cat', {
statics: {
speciesName: 'Cat' // My.Cat.speciesName = 'Cat'
},
constructor: function() {
alert(this.self.speciesName); // 依赖 'this'
},
clone: function() {
return new this.self();
}
});
Ext.define('My.SnowLeopard', {
extend: 'My.Cat',
statics: {
speciesName: 'Snow Leopard' // My.SnowLeopard.speciesName = 'Snow Leopard'
}
});
var cat = new My.Cat(); // alerts 'Cat' 猫
var snowLeopard = new My.SnowLeopard(); // alerts 'Snow Leopard' 雪豹
var clone = snowLeopard.clone();
alert(Ext.getClassName(clone)); // alerts 'My.SnowLeopard'
成员
调用原来的方法,这是以前的override重写
Ext.define('My.Cat', {
constructor: function() {
alert("I'm a cat!");
}
});
My.Cat.override({
constructor: function() {
alert("I'm going to be a cat!");
this.callOverridden();
alert("Meeeeoooowwww");
}
});
var kitty = new My.Cat(); // alerts "I'm going to be a cat!"
// alerts "I'm a cat!"
// alerts "Meeeeoooowwww"
This method has been deprecated since 4.1
版本 使用 callParent 代替.
参数的参数,数组或'参数'对象
来自当前方法,例如: this.callOverridden(arguments)
返回调用重写方法的结果。
所谓的"parent"方法是指当前的方法。 这是以前的方法派生或重写(参见 Ext.define)。
Ext.define('My.Base', {
constructor: function (x) {
this.x = x;
},
statics: {
method: function (x) {
return x;
}
}
});
Ext.define('My.Derived', {
extend: 'My.Base',
constructor: function () {
this.callParent([21]);
}
});
var obj = new My.Derived();
alert(obj.x); // alerts 21
这可以用来重写如下:
Ext.define('My.DerivedOverride', {
override: 'My.Derived',
constructor: function (x) {
this.callParent([x*2]); // 调用原来的My.Derived构造
}
});
var obj = new My.Derived();
alert(obj.x); // 现在提示 42
This also works with static methods.
Ext.define('My.Derived2', {
extend: 'My.Base',
statics: {
method: function (x) {
return this.callParent([x*2]); // 调用 My.Base.method
}
}
});
alert(My.Base.method(10); // alerts 10
alert(My.Derived2.method(10); // alerts 20
然后,它也可以重写静态方法。
Ext.define('My.Derived2Override', {
override: 'My.Derived2',
statics: {
method: function (x) {
return this.callParent([x*2]); // 调用 My.Derived2.method
}
}
});
alert(My.Derived2.method(10); // 现在提示 40
这个参数, 通过当前方法得到数组或者 arguments
对象,
例如: this.callParent(arguments)
返回调用父类的方法的结果。
这个类的初始化配置。典型例子:
Ext.define('My.awesome.Class', {
// 这是默认配置
config: {
name: 'Awesome',
isAwesome: true
},
constructor: function(config) {
this.initConfig(config);
}
});
var awesome = new My.awesome.Class({
name: 'Super Awesome'
});
alert(awesome.getName()); // 'Super Awesome' 超级棒
配置
mixins 混入原型 键-值对
获取从该对象被实例化的类的引用。 请注意不同于 self,
this.statics()
是独立的作用域,无论this
是否运行,总是返回其中的调用类。
Ext.define('My.Cat', {
statics: {
totalCreated: 0,
speciesName: 'Cat' // My.Cat.speciesName = 'Cat'
},
constructor: function() {
var statics = this.statics();
alert(statics.speciesName); // 总是等于'Cat',无论'this'是什么,
// 相当于:My.Cat.speciesName
alert(this.self.speciesName); // 依赖 'this'
statics.totalCreated++;
},
clone: function() {
var cloned = new this.self; // 依赖 'this'
cloned.groupName = this.statics().speciesName; // 相当于: My.Cat.speciesName
return cloned;
}
});
Ext.define('My.SnowLeopard', {
extend: 'My.Cat',
statics: {
speciesName: 'Snow Leopard' // My.SnowLeopard.speciesName = 'Snow Leopard'
},
constructor: function() {
this.callParent();
}
});
var cat = new My.Cat(); // alerts 'Cat', 然后提示 'Cat'
var snowLeopard = new My.SnowLeopard(); // alerts 'Cat', 然后提示 'Snow Leopard'
var clone = snowLeopard.clone();
alert(Ext.getClassName(clone)); // alerts 'My.SnowLeopard'
alert(clone.groupName); // alerts 'Cat'
alert(My.Cat.totalCreated); // alerts 3
配置扩展
方法/属性添加到这个类的原型。
Ext.define('My.awesome.Cat', {
constructor: function() {
...
}
});
My.awesome.Cat.implement({
meow: function() {
alert('Meowww...');
}
});
var kitty = new My.awesome.Cat;
kitty.meow();
成员
添加/重写这个类的静态属性。
Ext.define('My.cool.Class', {
...
});
My.cool.Class.addStatics({
someProperty: 'someValue', // My.cool.Class.someProperty = 'someValue'
method1: function() { ... }, // My.cool.Class.method1 = function() { ... };
method2: function() { ... } // My.cool.Class.method2 = function() { ... };
});
成员
this
这个类的原型借用另一个类的成员
Ext.define('Bank', {
money: '$$$',
printMoney: function() {
alert('$$$$$$$');
}
});
Ext.define('Thief', {
...
});
Thief.borrow(Bank, ['money', 'printMoney']);
var steve = new Thief();
alert(steve.money); // alerts '$$$'
steve.printMoney(); // alerts '$$$$$$$'
this 借用成员
创建这个类的新实例。
Ext.define('My.cool.Class', {
...
});
My.cool.Class.create({
someConfig: true
});
所有参数传递至类的构造。
创建的实例。
创建现有的原型方法的别名。例如:
Ext.define('My.cool.Class', {
method1: function() { ... },
method2: function() { ... }
});
var test = new My.cool.Class();
My.cool.Class.createAlias({
method3: 'method1',
method4: 'method2'
});
test.method3(); // test.method1()
My.cool.Class.createAlias('method5', 'method3');
test.method5(); // test.method3() -> test.method1()
别名新方法的名称,或对象设置多个别名。 参见flexSetter
原来的方法名
以字符串格式,获取当前类的名称。
Ext.define('My.cool.Class', {
constructor: function() {
alert(this.self.getName()); // alerts 'My.cool.Class'
}
});
My.cool.Class.getName(); // 'My.cool.Class'
className 类名
重写这个类的成员。通过callParent重写的方法可以调用。
Ext.define('My.Cat', {
constructor: function() {
alert("I'm a cat!");
}
});
My.Cat.override({
constructor: function() {
alert("I'm going to be a cat!");
this.callParent(arguments);
alert("Meeeeoooowwww");
}
});
var kitty = new My.Cat(); // alerts "I'm going to be a cat!我要成为一只猫!"
// alerts "I'm a cat!我是一只猫!"
// alerts "Meeeeoooowwww"
在4.1版本, 直接利用这种方法已经过时了。 使用 Ext.define 代替:
Ext.define('My.CatOverride', {
override: 'My.Cat',
constructor: function() {
alert("I'm going to be a cat!");
this.callParent(arguments);
alert("Meeeeoooowwww");
}
});
以上完成了相同的结果,但可以由Ext.Loader重写, 其目标类和生成过程中,可以决定是否需要根据目标类所需的状态覆盖管理(My.Cat)。
This method has been deprecated since 4.1.0
使用 Ext.define 代替
添加到这个类的属性。 这应当被指定为一个对象包含一个或多个属性的文字。
this class 当前类