最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Extend from custom model class in ExtJS 4 - Stack Overflow

programmeradmin3浏览0评论

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'}
    ],
});

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'}
    ],
});
Share Improve this question asked Oct 13, 2012 at 22:22 NasNas 8871 gold badge12 silver badges21 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 17

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

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'}
    ]);
   }
});
发布评论

评论列表(0)

  1. 暂无评论