Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active July 15, 2016 13:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shelob9/cbabf8a72c892f71ad27e738ffc2d7e0 to your computer and use it in GitHub Desktop.
Save Shelob9/cbabf8a72c892f71ad27e738ffc2d7e0 to your computer and use it in GitHub Desktop.
function api( url) {
var key = 'clCache' + url.replace(/^https?\:\/\//i, "");
var local;
//this line uses underscores.js!
if ( ! _.isString( local ) || "null" == local ) {
return $.get( url ).then( function ( r ) {
r = self.addPrefixes( r, prefix );
localStorage.setItem( key, JSON.stringify( r ) );
return r;
} );
}else {
return JSON.parse( local );
}
};
var posts1, posts2, posts;
$.get( 'https://calderawp.com/wp-json/wp/v2/posts' ).done( function(r ){
posts1 = r;
});
$.get( 'https://IngotHQ.com/wp-json/wp/v2/posts' ).done( function(r ){
posts2 = r;
});
//errors are about to happen
posts = posts1.concat( posts2 );
console.log( posts );
var posts;
$.when(
$.get( 'https://calderawp.com/wp-json/wp/v2/posts' ).done( function(r ){
return r;
}),
$.get( 'https://IngotHQ.com/wp-json/wp/v2/posts' ).done( function(r ){
posts2 = r;
}) ).then(
function( d1, d2 ) {
posts = d1[0].concat( d2[0] );
console.log( posts );
});
<div id="posts"></div>
<script type="text/html" id="tmpl--multiblog">
<article class="entry multi-blog-{{prefix}}">
<span class="image">
<img src="placeholder.png" alt="" class="img-{{prefix}}" data-media-id="{{featured_media}}" />
</span>
<a href="{{link}}" title="Read Full Post">
<h2>{{title.rendered}}</h2>
<div class="content">
<p>
{{excerpt.rendered}}
</p>
</div>
</a>
</article>
</script>
$.when( //one or more functions that return a promise ).then( function(){
//stuff to do after all promises are resolved
});
function merge(obj1, obj2) {
var merged;
a1 = _.toArray(obj1);
a2 = _.toArray(obj2);
merged = a1.concat(a2);
return merged;
};
function sort( list, sort ) {
var sorted = _.sortBy( list, sort );
return sorted.reverse();
};
var posts;
$.when( getCWP(), getIngot() ).then( function( d1, d2 ) {
var all = merge( d1, d2 );
all = sort( all, 'date' );
_.templateSettings = {
interpolate: /\{\{(.+?)\}\}/g
};
var html = '';
var templateHTML = document.getElementById( 'tmpl--multiblog' ).innerHTML;
var template = _.template( templateHTML );
var item, templatedItem;
$.each( all, function ( i, post ) {
templatedItem = template( post );
html += templatedItem;
});
document.getElementById( 'posts' ).innerHTML = html;
});
jQuery( document ).ready( function ( $ ) {
function getCWP() {
return api( 'https://calderawp.com/wp-json/wp/v2/posts' );
}
function getIngot() {
return self.api( 'https://ingothq.com/wp-json/wp/v2/posts' );
}
function api( url) {
var key = 'clCachez' + url.replace(/^https?\:\/\//i, "");
var local = localStorage.getItem( key );
//this line uses underscores.js!
if ( ! _.isString( local ) || "null" == local ) {
return $.get( url ).then( function ( r ) {
localStorage.setItem( key, JSON.stringify( r ) );
return r;
} );
}else {
return JSON.parse( local );
}
};
var posts;
$.when( getCWP(), getIngot() ).then( function( d1, d2 ) {
posts = d1.concat( d2 );
console.log( posts );
});
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment