Batch save javascript function

  • 1
  • Problem
  • Updated 2 years ago
  • In Progress
  • (Edited)
I have the below javascript snippet, which is designed to batch save records. I have very limited experience with using javascript, so what I have built below is based on information I could find in the Skuid community and support tutorials.

Any help with this would be appreciated.

var $ = skuid.$,
    models = skuid.model.map();

// Define the source model & save model 
var sourceModel = models.VendorChecklistVerify;
var saveModel = models.VendorChecklistSave;

function batchsave(){

    if (sourceModel.data.length > 10) {
    
    	var increment = 1;
    	
    	$.each(sourceModel.getRows(), function(i,row){	
    		
        		if (increment < 11){
        			
        		//duplicate this row in the Save Model
        		saveModel.adoptRow(row);
        		
        		//remove this row from the source model
        		sourceModel.abandonRow(row);
        		
        		//add to the increment
        		var increment = increment+1;
        		
        		} else{
        		
        		//save the "Save Model" and return to next batch
        		saveModel.save(batchsave());
        		}//end of If statement from line 16
        });//end of loop
    } else{
    	sourceModel.save();
    }//end of if statement from line 10
}//end of batchsave function
Photo of Brayden Smith

Brayden Smith

  • 1,078 Points 1k badge 2x thumb

Posted 2 years ago

  • 1
Photo of Brayden Smith

Brayden Smith

  • 1,078 Points 1k badge 2x thumb
^^bump^^
Photo of Greg Jarrett

Greg Jarrett

  • 3,496 Points 3k badge 2x thumb
Hi Brayden,

I'm not sure of the exact solution here but there's a couple of things that will definitely help you work it out:

- make use of console.log statements to log any errors (or to confirm expected values) as the function runs in the pag e you are testing (this assumes you're using Chrome as your browser and have opened the inspector on the skuid page which you are testing (right click, inspect, and click the console tab)
- no need to declare the 'increment' variable twice using 'var increment' - once you've declared it once its accessible to the rest of the code (or at least everything that isn't outside the block its declared in).

The below should help you get started:

var $ = skuid.$,
models = skuid.model.map();

// Define the source model & save model
var sourceModel = models.VendorChecklistVerify;
var saveModel = models.VendorChecklistSave;

function batchsave(){

console.log('Entered batchsave function');

if (sourceModel.data.length > 10) {

console.log('sourceModel length is ' + sourceModel.data.length);

var increment = 1;

$.each(sourceModel.getRows(), function(i,row){
console.log('current value of increment variable is ' + increment);
if (increment < 11){

//duplicate this row in the Save Model
saveModel.adoptRow(row);

//remove this row from the source model
sourceModel.abandonRow(row);

//add to the increment
//you should be able to increase the value here using the below statement
increment += 1;
//but console.log to check it anyway
console.log('new value of increment is ' + increment);
} else{

//save the "Save Model" and return to next batch
saveModel.save(batchsave());
}//end of If statement from line 16
});//end of loop
} else{
sourceModel.save();
}//end of if statement from line 10
}//end of batchsave function

Hope that helps, it should at least help you to identify what is working (and thus what isn't).