Extend from custom model class in ExtJS 4

ぃ、小莉子 提交于 2019-12-09 15:45:09

问题


How to extend from custom model in extjs.

Is there any method which can directly club the fields of User and BusinessUser fields when I'll refer the fields from BusinessUser class in example below.

Ext.define('User', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'name',  type: 'string'},
        {name: 'age',   type: 'int'},
        {name: 'phone', type: 'string'},
        {name: 'alive', type: 'boolean', defaultValue: true}
    ],
});

Ext.define('BusinessUser', {
    extend: 'User',
    fields: [
        {name: 'businessType',  type: 'string'},
        {name: 'company', type: 'string'}
    ],
});

回答1:


You don't need to join the fields manually because it's done automatically. Check the outputs in the code bellow based on your question:

Ext.define('User', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'name',  type: 'string'},
        {name: 'age',   type: 'int'},
        {name: 'phone', type: 'string'},
        {name: 'alive', type: 'boolean', defaultValue: true}
    ],
});

Ext.define('BusinessUser', {
    extend: 'User',
    fields: [
        {name: 'businessType',  type: 'string'},
        {name: 'company', type: 'string'}
    ],
});

// instantiating a User object
var u = Ext.create('BusinessUser', {
    name: 'John Doe', 
    age: 30, 
    phone: '555-5555'
});

// instantiating a BusinessUser object
var bu = Ext.create('BusinessUser', {
    name: 'Jane Doe', 
    age: 40, 
    phone: '555-5556', 
    businessType: 'analyst', 
    company: 'ACME'
});

console.log(Ext.getClassName(bu)); // "BusinessUser"
console.log(Ext.getClassName(u));  // "User"
console.log(u  instanceof User); // true
console.log(bu instanceof User); // true
console.log(u  instanceof BusinessUser); // false
console.log(bu instanceof BusinessUser); // true
console.log(u  instanceof Ext.data.Model); // true
console.log(bu instanceof Ext.data.Model); // true
console.log(u  instanceof Ext.data.Store); // false, just to check if it's not returning true for anything
console.log(bu instanceof Ext.data.Store); // false
console.log("name"    in u.data);  // true
console.log("name"    in bu.data); // true
console.log("company" in u.data);  // false
console.log("company" in bu.data); // true



回答2:


Although it should work automatically, use the below if you are having troubles for some reason.

Use the constructor to join the fields:

Ext.define('BusinessUser', {
   extend : 'User',
   constructor : function(){
      this.callParent(arguments);
      this.fields.push([
        {name: 'businessType',  type: 'string'},
        {name: 'company', type: 'string'}
    ]);
   }
});


来源:https://stackoverflow.com/questions/12877565/extend-from-custom-model-class-in-extjs-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!