Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend from custom model class in ExtJS 4

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'}
    ],
});
like image 226
Nas Avatar asked Oct 13 '12 22:10

Nas


2 Answers

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
like image 166
javalisson Avatar answered Oct 16 '22 21:10

javalisson


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'}
    ]);
   }
});
like image 40
Varun Achar Avatar answered Oct 16 '22 21:10

Varun Achar