TheOldBlackbeard
TheOldBlackbeard

Reputation: 423

Extjs add multiple records in the store

In my ExtJs app I have to save, after made some editing, multiple records in the store (as many records as it is the number of beaconsCounter) and after reload the store and close the dialog, but I don't know why, nothing happens. I am a newbie in Extjs so I don't know what it is incorrect in my code.

onSaveClick: function (button) {
              var dialog, store, beaconToSave , window;
              var numberOfBeaconPositionated, beaconsCounter, name, uuid, major, minor, originalNameValue;
              var arrayOfLong, arrayOfLat, nameString;

              window = button.up('window')
              dialog = window.down('form');
              dialog.updateRecord();
              beaconToSave = dialog.getRecord();
              store = beaconToSave.store;

              numberOfBeaconPositionated = this.lookupReference('beaconNumbers').getValue();
              name = beaconToSave.get('name');
              uuid = beaconToSave.get('uuid');
              major = beaconToSave.get('major');
              minor = beaconToSave.get('minor');
              latitude = this.lookupReference('beaconLatitude').getValue();
              longitude = this.lookupReference('beaconLongitude').getValue();

              if((name === "")||(uuid === "")){
                   Ext.Msg.alert(Strings.errorTitle, Strings.errorFieldMissing, Ext.emptyFn);
              }else if ((major === 0)||(minor === 0)){
                   Ext.Msg.alert(Strings.errorTitle, Strings.errorMajMin, Ext.emptyFn);
              }else if((longitude === "0")||(latitude === "0")){
                   Ext.Msg.alert(Strings.errorTitle, Strings.errorNoBeacons, Ext.emptyFn);
              }else{
                  nameString = name.split("-");

                  if((name.indexOf('-%min%') >= 0) || (name.indexOf('-%maj%') >= 0) || (name.indexOf('-'+major+'-'+minor) >= 0) || (nameString[1]!=null)) {
                     originalNameValue=nameString[0];
                  }else{
                     originalNameValue = name;
                  }

                  uuid = beaconToSave.get('uuid');
                  major = beaconToSave.get('major');
                  minor = beaconToSave.get('minor');
                  latitude = this.lookupReference('beaconLatitude').getValue();
                  longitude = this.lookupReference('beaconLongitude').getValue();
                  arrayOfLong = longitude.split(",");
                  arrayOfLat = latitude.split(",");
                  store = beaconToSave.store;
                  beaconsCounter = numberOfBeaconPositionated;

                  while(beaconsCounter > 0 ){

                      newRecord = beaconToSave.copy();
                      name=originalNameValue.concat("-"+major+"-"+minor);
                      newRecord.set('name',name);
                      newRecord.set('latitude',arrayOfLat[beaconsCounter-1]);
                      newRecord.set('longitude',arrayOfLong[beaconsCounter-1]);
                      newRecord.set('minor',minor);


                       if ( !store && !Ext.isEmpty(savedStore)){
                                 store = savedStore;
                             }
                             if (store) {
                                 if (newRecord.phantom) {
                                     store.add(newRecord);
                                 }
                                 store.sync({
                                     failure: function (batch) {
                                         store.rejectChanges();
                                         savedStore = store;
                                         if ( batch.exceptions[0].getError().status){
                                             if ( batch.exceptions[0].getError().status == 409){
                                                 Traccar.app.showError(this.getTitle() + Strings.errorAddDuplicate);
                                             }else if(batch.exceptions[0].getError().status == 400){
                                                 Traccar.app.showError(this.getTitle() + Strings.errorAddDatabase);
                                             }else{
                                                 Traccar.app.showError(batch.exceptions[0].getError().response);
                                             }
                                         }

                                     },
                                     success: function(){
                                         store.reload();
                                         this.close();
                                     }, scope: window
                                 });
                             } else {
                                 newRecord.save();
                                 this.closeView();
                             }
                      minor++;
                      beaconsCounter--;
                  }

              }
     },

Upvotes: 0

Views: 724

Answers (1)

norbeq
norbeq

Reputation: 3076

You shouldn't use newRecord = beaconToSave.copy() because this make new record with same id. Use newRecord = {'name':name,'latitude':arrayOfLat[beaconsCounter-1],'longitude',arrayOfLong[beaconsCounter-1],'minor',minor}; instead

Upvotes: 1

Related Questions