Skip to content

Example: handling user changed records (simulating math on user changes, change rippling to another record)

Tim Wood edited this page Feb 19, 2016 · 4 revisions

Handle a list of user-changed records that simulates doing some work on the data before saving:

  • Calculating a value based on the user-submitted data
  • Causing a change in a record the user didn't modify

To try it, call fake_user_changing_data():

	// The silly user changed some numbers!!!
	function fake_user_changing_data(){
		var raw_record_list = [];
		for( var i=0; i<4; i++ ) {
			raw_record_list[i] = {
				id		: i
			,	name	: 'Record ' + i
			};
		};
		console.log( 'You changed the data to ' );console.log( raw_record_list );
		handle_changes( raw_record_list );
	}
	
	/** Save all the change records and then display the results
	*/
	var handle_changes = function( raw_record_list ) {
		var record_list = doMathOnChangedData( raw_record_list  );
		
		// concurrently run the save changes
		// 	returned_records is what we're promised
		var returned_records = record_list.map(function( record ){
			// console.log( 'map: ' );console.log( record );
			return save_record( record ).then(function( returned_record ){
				// funnel it back to returned_records array
				return returned_record;
			}); 
		}); 
		
		return Q.all( returned_records ).done(function( a ){
			// print the record_list when the lookups and processing are done
			display_changes( returned_records );
		}); 
	}
	
	/**	Do calcs... including "updating" the record after the last user-updated one
	*/
	var doMathOnChangedData = function( record_list  ) {
		for( var i=0; i<record_list  .length; i++ ) {
			record_list[i].s = 
				record_list[i].id + 
				Math.floor(Math.random() * (100 + record_list[i].id)) + record_list[i].id;
		}
		id = record_list.length+1;
		record_list[ id ] = {
			id 		: id
		,	name	: 'Record ' + i
		, s		: 0
		};
		return record_list;
	}
	
	function saveUrl( record ) {
		var url = '/?id=' + record.id;
		for( var key in record ) {
			if( key == 'id' )	continue;
			url += '&' + key + '=' + record[key];
		}
		url = url.split(' ').join('%20');	// lazy cleanup
		// console.log( url );
		
		// okay... just in case your localhost go boom on the url
		url = '/';
		return url;
	}

	/** Save a changed record
	*/
	function save_record( _record ) {
		var record = _record;
		console.log( 'saving record: ' );console.log( record );
		var deferred = Q.defer();
		$.ajax( {
			url: saveUrl( record ),
			success: function( resultRecordIThink ) {
				// fake a return record
				var return_record = {};
				for( var key in record ) {
					return_record[key] = record[key];
				}
				return_record['bob'] = 'bob ' + return_record.id;
				var max = 100.0; var min =0.0;
				return_record.percent_done = Math.round( Math.random() * (max - min) + min, 2);
				
				console.log( 'save record ' + record.id + ' returned ' );console.log( return_record );
				deferred.resolve( return_record );
			}
		});
		
		return deferred.promise;
	}

	var display_changes = function( returned_records ) {
		console.log( 'returned_records (same as a): ' );console.log( returned_records );
	}