Last active
July 15, 2016 13:51
-
-
Save Shelob9/cbabf8a72c892f71ad27e738ffc2d7e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.when( //one or more functions that return a promise ).then( function(){ | |
//stuff to do after all promises are resolved | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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