2012-10-13 26 views
9

में कस्टम मॉडल क्लास से विस्तार करें extjs में कस्टम मॉडल से कैसे विस्तार करें।ExtJS 4

है वहाँ किसी भी विधि है जो कर सकते हैं सीधे क्लब उपयोगकर्ता और BusinessUser क्षेत्रों जब मैं नीचे दिए गए उदाहरण में BusinessUser वर्ग से क्षेत्रों पर गौर करेंगे के क्षेत्र।

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

उत्तर

15

आप मैन्युअल रूप से खेतों में शामिल होने क्योंकि यह कार्य स्वचालित रूप से जरूरत नहीं है। अपने प्रश्न के आधार पर कोड बेलो में आउटपुट देखें:

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 
+0

एक गलत टाइप की तरह लगता है, लेकिन ... 'console.log (Ext.getClassName (bu)); // "बिजनेस यूज़र" ' ' console.log (Ext.getClassName (u)); // "उपयोगकर्ता" ' 'बिजनेस यूज़र' दोनों बार प्रिंट करेगा और 'instanceof' के लिए परीक्षण तदनुसार आउटपुट प्रदान करेगा – bam

6

हालांकि इसे स्वचालित रूप से काम करना चाहिए, अगर आपको किसी कारण से परेशानी हो रही है तो नीचे दिए गए उपयोग करें।

क्षेत्रों में शामिल होने के निर्माता का उपयोग करें:

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

हालांकि इसे नीचे दिए गए उत्तर को सही करना चाहिए। कृपया इसे देखें क्योंकि यह आपके लिए स्वचालित रूप से करता है। – codemonkeyww

संबंधित मुद्दे