2012-06-07 23 views
7

मैं Extjs4 और एमवीसी का उपयोग करता हूं। मैं अपने मॉडल के क्षेत्रों को गतिशील रूप से बदलना चाहता हूं ... फ़ील्ड की एक चर संख्या जोड़ने की तरह कुछ ... कोई सुझाव देता है?Extjs4 मॉडल को गतिशील रूप से कैसे बदलें?

+1

के संभावित डुप्लिकेट [ExtJS के साथ गतिशील मॉडल 4] (http://stackoverflow.com/questions/5751289/dynamic-model-with-extjs-4) –

+0

वास्तव में क्या आप के लिए कोशिश कर रहे हैं कर? क्या तुम्हारे लिए थोडा और अधिक विशिष्ट होना संभव है? और आप एक्स्टजे का किस संस्करण का उपयोग कर रहे हैं? – sha

+1

Extjs4, उदाहरण के लिए मेरे पास 3 फ़ील्ड वाला मॉडल है ... मैं इसे अन्य छह फ़ील्ड के साथ विस्तारित करना चाहता हूं ... –

उत्तर

12

आप model.setFields(fieldsArray) फ़ंक्शन का उपयोग कर सकते हैं, जो API में here दिखाया गया है। यह विधि मॉडल पर मौजूद सभी मौजूदा फ़ील्ड को आपके द्वारा तर्क में शामिल किए गए नए फ़ील्ड के साथ बदल देती है। मौजूदा फ़ील्ड को कैप्चर करने के लिए एक स्थिर getFields विधि नहीं है ताकि उन्हें ओवरराइट न किया जा सके लेकिन model.prototype.fields का उपयोग करके उन्हें प्राप्त करना आसान हो गया है।

मैंने हाल ही में उपयोगकर्ता को लोड करने से पहले "उपयोगकर्ता" मॉडल में गतिशील अनुमति सेटिंग फ़ील्ड संलग्न करने के लिए यह किया था। यहाँ एक उदाहरण है:

Ext.define('myApp.controller.Main', { 
    extend: 'Ext.app.Controller', 

    models: [ 
     'User', 
    ], 

    stores: [ 
     'CurrentUser', // <-- this is not autoLoad: true 
     'PermissionRef', // <-- this is autoLoad: true 
    ], 

    views: ['MainPanel'], 

    init: function() { 
     var me = this; 

     // when the PermissionRef store loads 
     // use the data to update the user model 
     me.getPermissionRefStore().on('load', function(store, records) { 
      var userModel = me.getUserModel(), 
       fields = userModel.prototype.fields.getRange(); 
       // ^^^ this prototype function gets the original fields 
       // defined in myApp.model.User 

      // add the new permission fields to the fields array 
      Ext.each(records, function(permission) { 
       fields.push({ 
        name: permission.get('name'), 
        type: 'bool' 
       }); 
      }); 

      // update the user model with ALL the fields 
      userModel.setFields(fields); 

      // NOW load the current user with the permission data 
      // (defined in a Java session attribute for me) 
      me.getCurrentUserStore().load(); 

     }); 

    } 
}); 
+0

मैंने अभी देखा है कि यह जवाब देने के बाद [यह एक] (http://stackoverflow.com/questions/5751289/dynamic-model-with-extjs-4) का डुप्लिकेट है। मैंने वही जवाब दिया ... – Geronimo

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