Merge in all the branches
5 files changed, 46 insertions(+), 9 deletions(-)

M spec/model.spec.js
M src/datastore.js
M src/model.js
M src/null-datastore.js
M src/rest-service.js
M spec/model.spec.js +23 -0
@@ 337,6 337,29 @@ describe( 'Model class', () => {
 		} );
 	} );
 
+
+	describe( 'deleting', () => {
+
+		it( "deletes the object if the object has an id", () => {
+			let data = { id: 1234, firstName: "Morty", email: "morty@rm.co" };
+			let user = new User( data );
+
+			user.email = "mortimer@rm.co";
+
+			return expect( user.delete() ).to.be.fulfilled.then( () => {
+				expect( user.email ).to.equal( "mortimer@rm.co" );
+			} );
+
+		});
+
+
+		it( "resolves if the object does not have an id", () => {
+			let user = new User();
+
+			expect( user.delete.bind( user ) ).to.throw(/Cannot delete an object with no id/);
+		});
+
+	});
 } );
 
 

          
M src/datastore.js +1 -1
@@ 157,7 157,7 @@ export class Datastore {
 	 * @returns {Promise}  a promise that should resolve to `true` if the object
 	 *    was in the store prior to the removal, or `false` if it was not.
 	 */
-	remove( type, id ) {
+	remove( type, id, data ) {
 		return Promise.reject( new NotImplementedError("remove") );
 	}
 

          
M src/model.js +11 -3
@@ 287,19 287,27 @@ export class Model {
      * result.
 	 */
 	delete() {
+		let dirtyData = {};
+		for ( let field of this[ DIRTY_FIELDS ] ) {
+			dirtyData[ field ] = this[ DATA ][ field ];
+		}
+
 		if ( this.id ) {
-			return this[ DATASTORE ].remove( this.constructor, this.id ).
+			return this.validate().
+				then( () => {
+					this[ DATASTORE ].remove( this.constructor, this.id, dirtyData );
+				}).
 				then( deletedData => {
 					logger.debug( "Updating ", this, " with results from deletion." );
 					Object.assign( this[ DATA ], deletedData );
 					this[ NEW_OBJECT ] = true;
 					this[ DIRTY_FIELDS ].clear();
-					this.defineAttributes( this[DATA] );
+					this.defineAttributes( this[ DATA ] );
 
 					return this;
 				});
 		} else {
-			return Promise.resolve( this[ DATA ] );
+			throw new Error( "Cannot delete an object with no id" );
 		}
 	}
 

          
M src/null-datastore.js +9 -3
@@ 216,14 216,20 @@ export class NullDatastore extends Datas
 	 * @method remove
 	 * @param {Class}   type   the collection to remove the object from
 	 * @param {Integer} id     the ID of the object to remove
+	 * @param {Object}  data   the object data to store
 	 *
 	 * @returns {Promise} the promise that resolves to `true` if the object existed
 	 *                    or `false` if it did not.
 	 */
-	remove( type, id ) {
+	remove( type, id, data ) {
 		let collection = this.getCollectionForType( type );
-		let result = collection.delete( id );
-		return Promise.resolve( result );
+		collection.set( id, data );
+
+		let current = collection.get( id );
+
+		collection.delete( id );
+
+		return Promise.resolve( current );
 	}
 
 

          
M src/rest-service.js +2 -2
@@ 168,9 168,9 @@ export class RESTService extends Datasto
 	 * Delete the instance of the specified {type} with the given {id} via the REST service and
 	 * return a Promise that resolves to the result.
 	 */
-	remove( type, id ) {
+	remove( type, id, data ) {
 		let uri = `${type.uri}/${id}`;
-		return this.sendJsonRequest( uri, 'DELETE' );
+		return this.sendJsonRequest( uri, 'DELETE', data );
 	}