Instantly share code, notes, and snippets.

@Shelob9 /api-api.js
Last active Jul 15, 2016

Embed
What would you like to do?
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