2012-06-07 16 views
7

Ich verwende Extjs4 und MVC. Ich möchte die Felder meines Modells dynamisch ändern ... So etwas wie das Hinzufügen einer variablen Anzahl von Feldern ... Irgendwelche Vorschläge?Extjs4 Wie man ein Modell dynamisch ändert?

+1

möglich Duplikat [Dynamisches Modell mit ExtJS 4] (http://stackoverflow.com/questions/5751289/dynamic-model-with-extjs-4) –

+0

Was genau Sie versuchen, machen? Kannst du etwas genauer sein? Und welche Version von ExtJs benutzt du? – sha

+1

Extjs4, zum Beispiel habe ich ein Modell mit 3 Feld ... Ich möchte es mit anderen sechs Feld erweitern ... –

Antwort

12

Sie können die model.setFields(fieldsArray)-Funktion verwenden, die in der API here angezeigt wird. Diese Methode ersetzt alle vorhandenen Felder im Modell mit allen neuen Feldern, die Sie in das Argument aufnehmen. Es gibt keine statische getFields Methode, die vorhandenen Felder zu erfassen, um sie nicht zu überschreiben, aber es ist einfach genug, sie unter Verwendung model.prototype.fields zu erhalten.

Ich tat dies vor kurzem, um dynamische Berechtigungseinstellungsfelder an ein "Benutzer" -Modell anzuhängen, bevor ich den Benutzer geladen habe. Hier ein Beispiel:

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

Ich habe gerade bemerkt, dass dies ein Duplikat von [diesem] (http://stackoverflow.com/questions/5751289/dynamic-model-with-extjs-4) ist, nachdem ich es beantwortet habe. Ich gab genau die gleiche Antwort dort ... – Geronimo

Verwandte Themen