backbone-dynamodb.js
function putItem(model, options) {
	options || ( options = {} );
	if ( options.serializeDates !== false ) {
		// If serializeDates is NOT `false`, use `true` by default.
		options.serializeDates = true;
	}

	var newKey, // If the model `isNew`, a new key is generated.
		request,
		hashName, // Name of the hash attribute.
		rangeName, // Name of the range attribute.
		keyPromise,
		deferred = new _.Deferred(),
		/**
		 * Container object to store the attributes that are changed as part of saving the model.
		 */
		changed = {},
		/**
		 * Parameters to use in the DynamoDB PutItem request
		 */
		params = {
			/**
			 * Convert the model into an object.
			 * If `options.serializeDates` is `true`, `Date` values are converted into strings.
			 */
			Item: model.toJSON( options ),
			TableName: model._tableName()
		};

	if ( model.isNew() ) { // If the model is new, a new key attribute(s) must be generated
		hashName = _.result( model, 'hashAttribute' ) || _.result( model, 'idAttribute' );
		rangeName = _.result( model, 'rangeAttribute' );

		/**
		 * Convenience method to set the `newKey` attribute(s), once it's generated.
		 */
		function setNewKey(_newKey) {
			if ( rangeName ) {
				/**
				 * If the model has a range attribute, _newKey must be an object that contains
				 * the hash and range attributes.
				 */
				_.extend( changed, _newKey );
			} else {
				/**
				 * If the model doesn't have a range attribute, _newKey must be the value of the hash attribute.
				 */
				changed[ hashName ] = _newKey;
			}

			// Add the new key attribute(s) to the Item to be sent to DynamoDB
			_.extend( params.Item, options.serializeDates === true ? serializeAllDates( model, _.clone( changed ) ) : changed );
		}

		// Generate new key attribute(s)
		newKey = model.newKey( options );

		if ( isPromise( newKey ) ) {
			/**
			 * If `newKey` is a promise, wait until it's resolved to execute `setNewKey`
			 * and assign the resulting promise to `keyPromise`.
			 */
			keyPromise = newKey.then( setNewKey );
		} else {
			// If `newKey` is NOT a promise, call `setNewKey` right away.
			setNewKey( newKey );
		}
	}

	wrapComplete( model, options );
	_.extend( params, options.dynamodb );

	request = dynamo().putItem( params );

	request.on('complete', function (response) {
		var ctx = options.context || model;

		if ( response.error ) {
			deferred.rejectWith( ctx, [ response, options ] );
		} else {
			/**
			 * Backbone's "internal" success callback takes the changed attributes as the first argument.
			 * Make the entire AWS `response` available as `options.awsResponse`.
			 */
			options.awsResponse = response;
			deferred.resolveWith( ctx, [ changed, options ] );
		}
	});

	if ( !keyPromise ) {
		// If there's no `keyPromise`, send the request right away.
		request.send();
	} else {
		// If there's a `keyPromise`, wait until it is "done" before sending the request.
		keyPromise.done(function () {
			request.send();
		}).fail(function () {
			var ctx = options.context || model;
			deferred.rejectWith( ctx, arguments );
		});
	}

	deferred.done( options.success ).fail( options.error ).always( options.complete );

	return deferred.promise( request );
}
function putItem(model, options) {
	options || ( options = {} );
	if ( options.serializeDates !== false ) {
		// If serializeDates is NOT `false`, use `true` by default.
		options.serializeDates = true;
	}

	var newKey, // If the model `isNew`, a new key is generated.
		request,
		hashName, // Name of the hash attribute.
		rangeName, // Name of the range attribute.
		keyPromise,
		deferred = new _.Deferred(),
		/**
		 * Container object to store the attributes that are changed as part of saving the model.
		 */
		changed = {},
		/**
		 * Parameters to use in the DynamoDB PutItem request
		 */
		params = {
			/**
			 * Convert the model into an object.
			 * If `options.serializeDates` is `true`, `Date` values are converted into strings.
			 */
			Item: model.toJSON( options ),
			TableName: model._tableName()
		};

	if ( model.isNew() ) { // If the model is new, a new key attribute(s) must be generated
		hashName = _.result( model, 'hashAttribute' ) || _.result( model, 'idAttribute' );
		rangeName = _.result( model, 'rangeAttribute' );

		/**
		 * Convenience method to set the `newKey` attribute(s), once it's generated.
		 */
		function setNewKey(_newKey) {
			if ( rangeName ) {
				/**
				 * If the model has a range attribute, _newKey must be an object that contains
				 * the hash and range attributes.
				 */
				_.extend( changed, _newKey );
			} else {
				/**
				 * If the model doesn't have a range attribute, _newKey must be the value of the hash attribute.
				 */
				changed[ hashName ] = _newKey;
			}

			// Add the new key attribute(s) to the Item to be sent to DynamoDB
			_.extend( params.Item, options.serializeDates === true ? serializeAllDates( model, _.clone( changed ) ) : changed );
		}

		// Generate new key attribute(s)
		newKey = model.newKey( options );

		if ( isPromise( newKey ) ) {
			/**
			 * If `newKey` is a promise, wait until it's resolved to execute `setNewKey`
			 * and assign the resulting promise to `keyPromise`.
			 */
			keyPromise = newKey.then( setNewKey );
		} else {
			// If `newKey` is NOT a promise, call `setNewKey` right away.
			setNewKey( newKey );
		}
	}

	wrapComplete( model, options );
	_.extend( params, options.dynamodb );

	request = dynamo().putItem( params );

	request.on('complete', function (response) {
		var ctx = options.context || model;

		if ( response.error ) {
			deferred.rejectWith( ctx, [ response, options ] );
		} else {
			/**
			 * Backbone's "internal" success callback takes the changed attributes as the first argument.
			 * Make the entire AWS `response` available as `options.awsResponse`.
			 */
			options.awsResponse = response;
			deferred.resolveWith( ctx, [ changed, options ] );
		}
	});

	if ( !keyPromise ) {
		// If there's no `keyPromise`, send the request right away.
		request.send();
	} else {
		// If there's a `keyPromise`, wait until it is "done" before sending the request.
		keyPromise.done(function () {
			request.send();
		}).fail(function () {
			var ctx = options.context || model;
			deferred.rejectWith( ctx, arguments );
		});
	}

	deferred.done( options.success ).fail( options.error ).always( options.complete );

	return deferred.promise( request );
}
function putItem ( model , options ) { options || ( options = { } ) ; if ( options . serializeDates !== false ) { // If serializeDates is NOT `false`, use `true` by default. options . serializeDates = true ; } var newKey , // If the model `isNew`, a new key is generated. request , hashName , // Name of the hash attribute. rangeName , // Name of the range attribute. keyPromise , deferred = new _ . Deferred ( ) , /**
		 * Container object to store the attributes that are changed as part of saving the model.
		 */ changed = { } , /**
		 * Parameters to use in the DynamoDB PutItem request
		 */ params = { /**
			 * Convert the model into an object.
			 * If `options.serializeDates` is `true`, `Date` values are converted into strings.
			 */ Item : model . toJSON ( options ) , TableName : model . _tableName ( ) } ; if ( model . isNew ( ) ) { // If the model is new, a new key attribute(s) must be generated hashName = _ . result ( model , 'hashAttribute' ) || _ . result ( model , 'idAttribute' ) ; rangeName = _ . result ( model , 'rangeAttribute' ) ; /**
		 * Convenience method to set the `newKey` attribute(s), once it's generated.
		 */ function setNewKey ( _newKey ) { if ( rangeName ) { /**
				 * If the model has a range attribute, _newKey must be an object that contains
				 * the hash and range attributes.
				 */ _ . extend ( changed , _newKey ) ; } else { /**
				 * If the model doesn't have a range attribute, _newKey must be the value of the hash attribute.
				 */ changed [ hashName ] = _newKey ; } // Add the new key attribute(s) to the Item to be sent to DynamoDB _ . extend ( params . Item , options . serializeDates === true ? serializeAllDates ( model , _ . clone ( changed ) ) : changed ) ; } // Generate new key attribute(s) newKey = model . newKey ( options ) ; if ( isPromise ( newKey ) ) { /**
			 * If `newKey` is a promise, wait until it's resolved to execute `setNewKey`
			 * and assign the resulting promise to `keyPromise`.
			 */ keyPromise = newKey . then ( setNewKey ) ; } else { // If `newKey` is NOT a promise, call `setNewKey` right away. setNewKey ( newKey ) ; } } wrapComplete ( model , options ) ; _ . extend ( params , options . dynamodb ) ; request = dynamo ( ) . putItem ( params ) ; request . on ( 'complete' , function ( response ) { var ctx = options . context || model ; if ( response . error ) { deferred . rejectWith ( ctx , [ response , options ] ) ; } else { /**
			 * Backbone's "internal" success callback takes the changed attributes as the first argument.
			 * Make the entire AWS `response` available as `options.awsResponse`.
			 */ options . awsResponse = response ; deferred . resolveWith ( ctx , [ changed , options ] ) ; } } ) ; if ( ! keyPromise ) { // If there's no `keyPromise`, send the request right away. request . send ( ) ; } else { // If there's a `keyPromise`, wait until it is "done" before sending the request. keyPromise . done ( function ( ) { request . send ( ) ; } ) . fail ( function ( ) { var ctx = options . context || model ; deferred . rejectWith ( ctx , arguments ) ; } ) ; } deferred . done ( options . success ) . fail ( options . error ) . always ( options . complete ) ; return deferred . promise ( request ) ; }
Sends a request to store a Backbone.DynamoDB.Model in a DynamoDB table using a PutItem request.

@param {Backbone.DynamoDB.Model} model The model to store in DynamoDB.
@param {Object} options The options object.
Sends a request to store a Backbone . DynamoDB . Model in a DynamoDB table using a PutItem request .