All files / mobile.startup promisedView.js

100% Statements 13/13
100% Branches 4/4
100% Functions 3/3
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38  1x 1x                     4x     4x 4x 2x   2x   2x     1x     1x   1x     4x     1x  
var
	icons = require( './icons' ),
	View = require( './View' );
 
/**
 * It's a view that spins until the promise resolves!
 * If the promise successfully resolves, the newView will be shown. if the
 * promise rejects and rejects to a view, the errorView will be shown.
 *
 * @param {jQuery.Promise} promise
 * @return {View}
 */
function promisedView( promise ) {
	var view = new View( {
		className: 'promised-view'
	} );
	view.$el.append( icons.spinner().$el );
	promise.then( ( newView ) => {
		view.$el.replaceWith( newView.$el );
		// update the internal reference.
		view.$el = newView.$el;
	}, ( errorView ) => {
		if ( !errorView || !errorView.$el ) {
			// return early to keep backwards compatibility with clients of
			// promisedView that do not reject to an error view
			return;
		}
 
		view.$el.replaceWith( errorView.$el );
		// update the internal reference.
		view.$el = errorView.$el;
	} );
 
	return view;
}
 
module.exports = promisedView;