扩展Ext.data.Model的类-方法.get和.set的行为与getter/setter不同



当我通过扩展"Ext.data.model"类创建模型时,getter/setter方法的行为与数据中可用的默认.get和.set方法不同。型号

似乎可以使用getter/setter方法或.get/.set方法,因为它们似乎维护着单独的字段集。

为什么会这样?如果这个问题看起来很傻,请原谅,我正在学习ExtJS,并试图了解它是如何工作的。我使用的是库版本ExtJS4.2.1

Ext.define("Ext.model.Invoice", {
        extend : "Ext.data.Model",
        fields : [{name : 'id'}, {name : 'taxId'}, {name : 'name'}],
        config : {
            name : 'Tejas',
            taxId : '23746'
        },
        constructor : function(config) {
            this.callParent(arguments);
            this.initConfig(config);
        }
    });

HTML

Ext.onReady(function() {
    var invoice = Ext.create("Ext.model.Invoice");
    console.log("Before, invoice.get('name'):", invoice.get('name'));
    console.log("Before, invoice.getName():", invoice.getName());
    //Modifying name
    invoice.setName("Mr. Smith");
    invoice.set("name","Mr. Tony");
    console.log("Updating names using setName and set('name')");
    console.log("After, invoice.get('name'):", invoice.get('name'));
    console.log("After, invoice.getName():", invoice.getName());
});

输出

Before, invoice.get('name'):
Before, invoice.getName(): Tejas
Updating names using setName and set('name')
After, invoice.get('name'): Mr. Tony
After, invoice.getName(): Mr. Smith

使用config配置属性,您定义的是具有默认值的配置选项列表,而不是默认模型数据。

当创建对象的实例时,为config中定义的每个属性自动创建setter和getter方法,以及与config属性同名的对象属性。

Ext.data.Model将模型数据存储在其私有data属性中。例如,您可以尝试通过以下方式转储name字段的模型数据:

console.log(invoice.data.name);

所以通过setter和getter可以访问对象属性,但通过model.get()model.set()可以访问存储在模型私有data属性中的模型数据。

最新更新