Webinar: AngularJS and the WordPress REST API

31
Technical Deep Dive into AngularJS and the WordPress REST API March 16 th , 2016 #wprestapi Vote for your favorite plugins: www.pluginmadness.com

Transcript of Webinar: AngularJS and the WordPress REST API

Page 1: Webinar: AngularJS and the WordPress REST API

Technical Deep Dive into AngularJS and the

WordPress REST API

March 16th, 2016

#wprestapi

Vote for your favorite plugins:www.pluginmadness.com

Page 2: Webinar: AngularJS and the WordPress REST API

Ask Questions as We Go!

Please use the “Questions”pane throughout the webinar

#wprestapi

Slides and recording will be made available shortly after the webinar

Page 3: Webinar: AngularJS and the WordPress REST API

What We’ll Discuss

Building on last webinar: The WP REST API as a Springboard for Website

Greatness

❖ How to make custom admin interfaces using REST API and AngularJS

❖ Angular basics with the WordPress REST API❖ Build a plugin admin screen (Ingot A/B testing)

#wprestapi

Page 4: Webinar: AngularJS and the WordPress REST API

Quick Intros!

#wprestapi

Josh Pollock Owner/Developer, CalderaWP and Ingot@josh412

❖ CalderaWP.com

❖ IngotHQ.com

❖ JoshPress.net

Anthony Burchell Operations Engineer, WP Engine@antpb

❖ Started on WordPress 2.8

❖ Casual Core Contributor

❖ Antpb.com

❖ Synth nerd

Page 5: Webinar: AngularJS and the WordPress REST API

Is this the new way?

Page 6: Webinar: AngularJS and the WordPress REST API

What is the benefit?● Respects standards &

separation of concerns● Relatively easy to learn● Testable● Someone else pays to

maintain it. #thanksgoogle

Page 7: Webinar: AngularJS and the WordPress REST API

But admin-ajax!

Page 8: Webinar: AngularJS and the WordPress REST API

Custom or Default Routes

#wprestapi

Use Default Routes

❖ Install the plugin

❖ https://wordpress.org/plugins/rest-api/

Make Your Own Endpoints

❖ Make your own API with WordPress 4.4+

❖ Talk: video, slides & links

❖ Torque Article

Page 9: Webinar: AngularJS and the WordPress REST API

Part One

MVC

Page 10: Webinar: AngularJS and the WordPress REST API

MVCMVC●Model●View●Controller

Page 11: Webinar: AngularJS and the WordPress REST API

MVCModelThe model is the current set of data, defined by the controller,

displayed by the view.

Page 12: Webinar: AngularJS and the WordPress REST API

MVC$scopeCurrent state of the model. Defined in controller - used in

view.

Page 13: Webinar: AngularJS and the WordPress REST API

MVCView● The visual representation of

the data.● In Angular this is an HTML

file.

Page 14: Webinar: AngularJS and the WordPress REST API

MVCController● Keeps the models up-to-

date using the remote API.● Updates the model based

on your interactions with the view.

Page 15: Webinar: AngularJS and the WordPress REST API

Part Two

Bindings

Page 16: Webinar: AngularJS and the WordPress REST API

MVCBindings● Connects views to

controllers.● HTML5 Attributes

● Template Tags: Curly Brackets

<div ng-controller="postExample"> <form> <input type="text" ng-model="post.title" /> </form> <div>{{post.title}}</div></div>

Controller

Page 17: Webinar: AngularJS and the WordPress REST API

MVCBindings● Use controller function to

create controller...● $scope is available in view

Template(function(angular) { 'use strict'; angular.module('learnAngular', []) .controller('postExample', ['$scope', function($scope) { $scope.post = { title: 'Enter Title' };

}]);})(window.angular);

Page 18: Webinar: AngularJS and the WordPress REST API

MVCBindings● Bindings can be used to call

functions● Examples:

○ ng-submit○ ng-hide

Views<div ng-controller="postExample"> <form ng-submit="submit()"> <input type="text" ng-model="post.title" /> <input type="submit" value="Save" ng-hide="post.title == 'Enter Title'" /> </form> <div>{{post.title}}</div></div>

Page 19: Webinar: AngularJS and the WordPress REST API

MVCBindings● Define functions for view on

$scope.● Example: $scope.submit

Controller(function(angular) { 'use strict'; angular.module('learnAngular', []) .controller('postExample', ['$scope', function($scope) { $scope.post = { title: 'Enter Title' }; $scope.submit = function(){ alert( 'saving' ); } }]);})(window.angular);

Page 20: Webinar: AngularJS and the WordPress REST API

MVCBindings● ng-repeat:

○ Repeat items (like a list of posts)

Views<div ng-controller="postsExample"> <h3>Posts:</h3> <div ng-repeat="post in posts"> {{post.title}} </div></div>

Page 21: Webinar: AngularJS and the WordPress REST API

MVCBindings● Look mom, two controllers!● Iterating over posts array.

(function(angular) { 'use strict'; angular.module('learnAngular', []) .controller('postExample', ['$scope', function($scope) { $scope.post = { title: 'Enter Title' }; $scope.submit = function(){ alert( 'saving' ); } }]).controller('postsExample', ['$scope', function($scope) { $scope.posts = [ { title: 'Post One' }, { title: 'Post Two' } ]; }]);})(window.angular);

Page 22: Webinar: AngularJS and the WordPress REST API

Making your own Super Fast Super Fancy Admin Interface!

Or….

Page 23: Webinar: AngularJS and the WordPress REST API

SFSFAI!This is going to catch on...

Making your own Super Fast Super Fancy Admin Interface!

Page 24: Webinar: AngularJS and the WordPress REST API

MVCAngular UI Router

● What URL uses what controller and template?

● http://jpwp.me/ingot-router

ingotApp.config(function($stateProvider, $urlRouterProvider) { $urlRouterProvider.otherwise("/"); $stateProvider //click tests .state('clickTests', { url: "/click-tests", templateUrl: INGOT_ADMIN.partials + "/click-groups.html" }) .state('clickTests.list', { url: "/click-tests/all", templateUrl: INGOT_ADMIN.partials + "/list.html", controller: 'clickGroups' } ) .state('clickTests.edit', { url: "/click-tests/edit/:groupID", templateUrl: INGOT_ADMIN.partials + "/click-group.html", controller: 'clickGroup' } ) .state('clickTests.new', { url: "/click-tests/new", templateUrl: INGOT_ADMIN.partials + "/click-group.html", controller: 'clickGroup' } )});

Page 25: Webinar: AngularJS and the WordPress REST API

MVCStart it up● Include dependencies● Adding translations to

$rootScope

var ingotApp = angular.module('ingotApp', [ 'ui.router', 'ui.bootstrap', 'colorpicker.module', 'ngAria', 'ngResource', 'ngclipboard', 'ngSanitize'] ) .run( function( $rootScope, $state ) { $rootScope.translate = INGOT_TRANSLATION; $rootScope.partials_url = INGOT_ADMIN.partials; });

Page 26: Webinar: AngularJS and the WordPress REST API

MVCAngular $http

● Similar to jQuery AJAX● Use to update $scope and

$state

ingotApp.controller( 'clickDelete', ['$scope', '$http', '$stateParams', '$state', function( $scope, $http, $stateParams, $state ){ $http({ url: INGOT_ADMIN.api + 'groups/' + $stateParams.groupID + '?_wpnonce=' + INGOT_ADMIN.nonce, method:'DELETE', headers: { 'X-WP-Nonce': INGOT_ADMIN.nonce } } ).then( function successCallback() { swal( INGOT_TRANSLATION.deleted, "", "success" ); $scope.group = {}; $state.go('clickTests.list' ); }, function errorCallback( response ) { var data = response.data; var text = INGOT_TRANSLATION.sorry; if( _.isObject( data ) && _.isDefined( data.message ) ){ text = data.message; } $state.go('clickTests.list' ); } );}]);

Page 27: Webinar: AngularJS and the WordPress REST API

MVCFactories● Reusable code for HTTP● Makes data an injected

dependency -- easily mocked/ modified

● http://jpwp.me/ingot-factory

ingotApp.factory( 'groupsFactory', function( $resource ) {return $resource( INGOT_ADMIN.api + 'groups/:id',

{id: '@id',

_wpnonce: INGOT_ADMIN.nonce, context: 'admin'

},{'query' : {

transformResponse: function( data, headers ) {

var response = {

data: data,

headers: headers()};return

response;}

}, 'update':{ method:'PUT', headers: { 'X-WP-Nonce': INGOT_ADMIN.nonce } }, })});

Page 28: Webinar: AngularJS and the WordPress REST API

MVCFactories● Think of it as your own API

client●http://jpwp.me/ingot-factory-i

n-use

ingotApp.controller( 'clickGroups', ['$scope', '$http', 'groupsFactory', '$sce', function( $scope, $http, groupsFactory, $sce ) { var page_limit = 10; $scope.description = $sce.trustAsHtml( INGOT_TRANSLATION.descriptions.click ); groupsFactory.query( { page: 1, limit: page_limit, context: 'admin', type: 'click' }, function ( res ) { if ( res.data.indexOf( 'No matching' ) > -1 ) { $scope.groups = {}; return; }; $scope.groups = JSON.parse( res.data ); var total_groups = parseInt( res.headers[ 'x-ingot-total' ] ); var total_pages = total_groups / page_limit; $scope.total_pages = new Array( Math.round( total_pages ) ); $scope.groups.shortcode = []; } );}]);

Page 29: Webinar: AngularJS and the WordPress REST API

Resources

❖eBook: The Ultimate Guide to the WordPress REST APIhttp://wpeng.in/rest-api-ebook/

❖Article: Basics of AngularJS with the WordPress REST APIhttp://torquemag.io/2016/02/basics-of-angularjs-with-the-wordpress-rest-api/

❖Building Apps with the WordPress REST APIhttps://learn.joshpress.net/downloads/building-apps-wordpress-rest-api/

❖Creating a Javascript Single Page App in the WordPress Dashboardhttp://torquemag.io/2015/12/creating-javascript-single-page-app-wordpress-dashboard/ #wprestapi

Page 30: Webinar: AngularJS and the WordPress REST API

CalderaWPREST API CourseCalderaWP.com

Q&A

Feel free to ask away inthe “Questions” pane!

Are you an agency or freelancer?

● Finish more sites in less time● Unlimited staging installs for WordPress projects for

as little as $29 per month.

Ask about qualifying for a listing in our online consultants’ directory!

Call +1-512-827-3500, orChat with us wpengine.com

Page 31: Webinar: AngularJS and the WordPress REST API

Myths, Mistakes & Management of WooCommerce at Scale

Wednesday, April 13th, 12 pm EDT/ 11 am CDT/ 9 am PDT/ 5 pm GMT+1

Register Now! http://wpeng.in/woo/

❖ Myths associated with scaling WooCommerce

❖ Mistakes and how to avoid them❖ How to pick development and hosting

partners

Next

Next Webinar