Last active
August 5, 2016 11:29
-
-
Save machito/8d31a5d7d43679af0cbc to your computer and use it in GitHub Desktop.
Custom AngularJS + WordPress API post/page preview functionality
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
'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); | |
}); |
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
'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); | |
}); | |
}); |
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
'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