Skip to content

Instantly share code, notes, and snippets.

@machito
Last active August 5, 2016 11:29
Show Gist options
  • Save machito/8d31a5d7d43679af0cbc to your computer and use it in GitHub Desktop.
Save machito/8d31a5d7d43679af0cbc to your computer and use it in GitHub Desktop.
Custom AngularJS + WordPress API post/page preview functionality
'use strict';
var app = angular.module('app', [
'ui.router',
'ngResource',
'ngSanitize'
]);
app.config(function($stateProvider, $urlRouterProvider, $locationProvider, $httpProvider) {
$urlRouterProvider.otherwise('/');
$stateProvider
.state('index', {
url: '/',
templateUrl: 'html/views/main.html',
controller: 'MainCtrl'
})
.state('events', {
url: '/calendar-of-events/?preview&preview_id&preview_nonce',
templateUrl: 'html/views/events.html',
controller: 'EventsCtrl'
});
$locationProvider.html5Mode(true);
});
'use strict';
var app = angular.module('app');
app.controller('EventsCtrl', function ($scope,$stateParams, eventsFactory) {
$scope.preview = $stateParams.preview;
$scope.preview_id = $stateParams.preview_id;
$scope.preview_nonce = $stateParams.preview_nonce;
eventsFactory.getSingle(
{ preview: $scope.preview,
preview_id: $scope.preview_id,
preview_nonce: $scope.preview_nonce
},
function(data){
// code goes here
}, function(err){
console.error(err);
});
});
'use strict';
angular.module('app')
.factory('eventsFactory', function ($resource, CONFIG) {
return $resource('http://api.example.com/events/',
{ callback: 'JSON_CALLBACK',
preview: '@preview',
preview_id: '@preview_id',
preview_nonce: '@preview_nonce'
}, {
getSingle: {method: 'JSONP', params: { preview: '', preview_id: '', preview_nonce: '' } }
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment