Browse Source

wrap wp.api in loadPromise so schema is localized

windhamdavid 8 years ago
parent
commit
84781e6cca
1 changed files with 39 additions and 38 deletions
  1. 39 38
      js/loop.js

+ 39 - 38
js/loop.js

@@ -20,6 +20,7 @@
 	var page = 1;
 	var timer;
 
+wp.api.loadPromise.done( function() {
 	var posts = new wp.api.collections.Posts();
 	var options = {
 		data: {
@@ -40,6 +41,44 @@
 			author: settings.queriedObject.data.ID
 		};
 	}
+	
+	/**
+	 * Initial posts fetch
+	 */
+	posts.fetch( options ).done( function() {
+
+		if ( posts.length > 0 ) {
+			$postContainer.append( $moreButton );
+
+			setupMoreListener();
+			setupScrollListener();
+		}
+	});
+	
+	/**
+	 * Grab more posts if more button is clicked and append them to loop
+	 */
+	function setupMoreListener() {
+		$postContainer.on( 'click', '.more-button', function( event ) {
+			event.preventDefault();
+
+			$moreButton.hide();
+
+			var $setContainer = $( '<div data-page-num="' + posts.state.currentPage + '" class="post-set"></div>' );
+			posts.each( function( model ) {
+				$setContainer.append( postTemplate( { post: model.attributes, settings: settings } ) );
+			});
+
+			$postContainer.append( $setContainer );
+
+			if ( posts.hasMore() ) {
+				posts.more().done( function() {
+					$moreButton.appendTo( $postContainer).show();
+				} );
+			}
+		});
+	};
+});
 
 	/**
 	 * Update current url using HTML5 history API
@@ -57,7 +96,6 @@
 
 	/**
 	 * Determine URL for pushing new history. Props to Automattic's Jetpack plugin
-	 * for much of this code.
 	 */
 	function determineURL() {
 		var windowTop = $( window ).scrollTop();
@@ -152,41 +190,4 @@
 		});
 	}
 
-	/**
-	 * Grab more posts if more button is clicked and append them to loop
-	 */
-	function setupMoreListener() {
-		$postContainer.on( 'click', '.more-button', function( event ) {
-			event.preventDefault();
-
-			$moreButton.hide();
-
-			var $setContainer = $( '<div data-page-num="' + posts.state.currentPage + '" class="post-set"></div>' );
-			posts.each( function( model ) {
-				$setContainer.append( postTemplate( { post: model.attributes, settings: settings } ) );
-			});
-
-			$postContainer.append( $setContainer );
-
-			if ( posts.hasMore() ) {
-				posts.more().done( function() {
-					$moreButton.appendTo( $postContainer).show();
-				} );
-			}
-		});
-	};
-
-	/**
-	 * Initial posts fetch
-	 */
-	posts.fetch( options ).done( function() {
-
-		if ( posts.length > 0 ) {
-			$postContainer.append( $moreButton );
-
-			setupMoreListener();
-			setupScrollListener();
-		}
-	});
-
 })( jQuery, Backbone, _, settings );