Skip to content

Instantly share code, notes, and snippets.

@jcicero518
Forked from dannyockilson/gist:52a444195f0df873cc1c
Last active August 29, 2015 14:20
Show Gist options
  • Save jcicero518/4015783d6ec017a31dac to your computer and use it in GitHub Desktop.
Save jcicero518/4015783d6ec017a31dac to your computer and use it in GitHub Desktop.
'use strict';
angular.module('wordpress', [])
.service( 'wpService',
function($http, $q){
var url = 'http://allin.local/wp-json/';
return({
base: base,
custom: custom,
types: types,
taxonomies: taxonomies,
terms: terms,
term: term,
page: page,
getByType: getByType,
getBySlug: getBySlug
})
function base() {
return ( $http.get(url)
.then( handleSuccess, handleError ) );
}
function custom( path ) {
return ( $http.get(url+path)
.then( handleSuccess, handleError ) );
}
function types() {
return ( $http.get(url+'posts/types')
.then( handleSuccess, handleError ) );
}
function taxonomies() {
return ( $http.get(url+'taxonomies')
.then( handleSuccess, handleError ) );
}
function terms( tax ) {
return ( $http.get(url+'taxonomies/'+tax+'/terms')
.then( handleSuccess, handleError ) );
}
function term( tax, term ) {
return ( $http.get(url+'taxonomies/'+tax+'/terms/'+term)
.then( handleSuccess, handleError ) );
}
function page( id ) {
return ( $http.get(url+'posts/'+id)
.then( handleSuccess, handleError ) );
}
function getByType( type, limit ) {
return ( $http.get(url+'posts?type[]='+type+'&filter[posts_per_page]='+limit)
.then( handleSuccess, handleError ) );
}
function getBySlug( type, slug ) {
return ( $http.get(url+'posts?type[]='+type+'&filter[posts_per_page]=1&filter[name]='+slug)
.then( handleSuccess, handleError ) );
}
function handleSuccess( response ) {
return( response.data );
}
function handleError( response ) {
if ( ! angular.isObject( response.data ) || ! response.data.message ) {
return( $q.reject( "An unknown error occurred." ) );
}
return( $q.reject( response.data.message ) );
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment