Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4
Getting Some Closure
aka Best Practices for JavaScript and jQuery in WordPress Vasken Hauri
Getting Some Closure aka Best Practices for JavaScript and jQuery - - PowerPoint PPT Presentation
Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4 Getting Some Closure aka Best Practices for JavaScript and jQuery in WordPress Vasken Hauri Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4 About me VP of
Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4
aka Best Practices for JavaScript and jQuery in WordPress Vasken Hauri
Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4
interested (or check out http://10up.com).
accurately named.
Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4
Better than a double rainbow!
Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4
(CRUD) your WordPress content via a RESTful API.
data and presentation layers becomes increasingly important.
the power to manage, edit, and display your content with the highest degree of flexibility and performance.
Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4
Collage courtesy of Taylor Lovett Source: http://taylorlovett.com/2014/08/23/ wordcamp-boston-2014-wp-api/
Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4
Remote Address:192.168.50.4:80 Request URL:http://local.wordpress.dev/wp-json/posts/1 Request Method:GET Status Code:200 OK
{ "ID": 1, "title": "Hello world!", "status": "publish", "type": "post", "author": { "ID": 1, "username": "admin", "name": "admin", […] "meta": { "links": { "self": "http://local.wordpress.dev/wp-json/users/1", "archives": "http://local.wordpress.dev/wp-json/users/1/posts" } } }, "content": "<p>Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!</p>\n", …
Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4
server and implemented in a template via JS.
engine (Mustache, Handlebars, etc.).
places, by supporting OAuth and ofgering simple interactivity with mobile or web apps.
JavaScript.
Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4
with other plugins //You wouldn't do this in WordPress...would you? function get_the_ID() { echo 'hello, I am a giant function name collision!'; }
Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4
name collisions, but it’s not 100% efgective.
abbreviations could cause accidental collisions on some installs. //This is a bit better function my_cool_plugin_get_the_ID() { echo 'hello, I am a decent option for avoiding function name collisions.'; }
Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4
example: a globally namespaced function attached to the window object.
by other plugins. function getPost(thePostID) { var postID = thePostID, post = {}; //Get a post through the WP JSON API return postID; }
Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4
//Object-oriented PHP enables us to avoid function name collisions class My_Cool_Plugin { function __construct() { //do stuff to instantiate the class } public function get_the_ID() { echo 'hello, I play nice in the global namespace because I\'m in a class.'; } }
global $myplugin; $myplugin = new My_Cool_Plugin(); //...or using a singleton, or whatever you'd like!
Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4
//Let's retrieve a post! $new_post = get_post( 7 );
WP_Post object! $new_post = get_post( 7 );
Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4
limiting calls to the database or caching layer. This is good practice for PHP performance.
equivalent of the database/caching layer: reducing calls to it is good practice for performance.
//That doesn't make much sense...we could just do: $new_post = get_post( 7 ); echo $new_post->post_title; echo $new_post->post_type; //This results in fewer DB queries (or cache key calls)
Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4
DOM, which takes far longer than retrieving the result of the jQuery $() function from a variable.
Often, this means convenience over performance. //Section 2: Performance Optimization $( '#main' ).addClass( 'visible' );
$( '#main' ).removeClass( 'visible' ); //This is non-performant (multiple DOM calls and Sizzle use)
Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4
//The performant option is... var $main = $( document.getElementById( 'main' ) );
$main.removeClass( 'visible' ); //This is more performant (single DOM call and built-in JS selectors)
Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4
Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4
//This is an example closure called myCoolPlugin function myCoolPlugin() {
var theID = 1; //This function is only available within the closure, similar to a private function in PHP function privateFunction () {
}
return { getID: function () { return theID; }, setID: function (newID) { theID = newID; } }; }
Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4
//we instantiate the closure by calling our closure function var coolPluginInstance = myCoolPlugin();
console.log(coolPluginInstance.getID());
//within this instance of the closure coolPluginInstance.setID(2);
console.log(coolPluginInstance.getID());
Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4
//let's create another instance of the closure var coolPluginInstance2 = myCoolPlugin();
console.log(coolPluginInstance2.getID());
Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4
Closures ofger a lot of the same benefits as PHP classes:
the closure.
variable, creating an “instance” of the closure.
plugins’ namespaces.
Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4
and the fact that they might not have the latest hardware: write performant JavaScript.
the WordPress ecosystem, and do your part to limit plugin collisions.
whenever possible.
Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4
planned for late 2014/early 2015).
publishing sites, as a plugin.
gaining universal adoption as a data exchange format, the sky’s the limit in terms of integrating with other web technologies and applications.
Vasken Hauri | @vaskenhauri #WP-API | http://tinyurl.com/lfgsqc4