9

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'}
    ],
});
Nas
  • 887
  • 1
  • 11
  • 21

2 Answers2

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
javalisson
  • 196
  • 1
  • 7
  • Seem like a mistype, but... `console.log(Ext.getClassName(bu)); // "BusinessUser"` `console.log(Ext.getClassName(u)); // "User"` will print `BusinessUser` both times and tests for `instanceof` will provide output accordingly – bam Sep 12 '16 at 10:25
6

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'}
    ]);
   }
});
Varun Achar
  • 14,781
  • 7
  • 57
  • 74
  • Though this should work the answer below is the correct one. Please refer to it since it does this for you automatically. – b3labs Mar 26 '13 at 11:17