Skip to content

Commit

Permalink
Convert scripts to es6
Browse files Browse the repository at this point in the history
  • Loading branch information
QWp6t committed Jul 16, 2016
1 parent eae36be commit f34af48
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 38 deletions.
13 changes: 5 additions & 8 deletions assets/scripts/customizer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
(function($) {
// Site title
wp.customize('blogname', function(value) {
value.bind(function(to) {
$('.brand').text(to);
});
});
})(jQuery);
import $ from 'jquery';

wp.customize('blogname', (value) => {
value.bind((to) => $('.brand').text(to))
});
33 changes: 8 additions & 25 deletions assets/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import $ from 'jquery';
import Router from './util/router';
import Common from './routes/Common';
import Home from './routes/Home';
import About from './routes/About';

// Import Bootstrap
import 'bootstrap/dist/js/umd/util.js';
Expand All @@ -16,34 +19,14 @@ import 'bootstrap/dist/js/umd/popover.js';

// Use this variable to set up the common and page specific functions. If you
// rename this variable, you will also need to rename the namespace below.
var Sage = {
const routes = {
// All pages
'common': {
init: function() {
// JavaScript to be fired on all pages
},
finalize: function() {
// JavaScript to be fired on all pages, after page specific JS is fired
}
},
'common': Common,
// Home page
'home': {
init: function() {
// JavaScript to be fired on the home page
},
finalize: function() {
// JavaScript to be fired on the home page, after the init JS
}
},
'home': Home,
// About us page, note the change from about-us to about_us.
'about_us': {
init: function() {
// JavaScript to be fired on the about us page
}
}
'about_us': About
};

// Load Events
$(document).ready(function() {
new Router(Sage).loadEvents();
});
$(document).ready(() => new Router(routes).loadEvents());
5 changes: 5 additions & 0 deletions assets/scripts/routes/About.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
init() {
// JavaScript to be fired on the about us page
}
};
8 changes: 8 additions & 0 deletions assets/scripts/routes/Common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
init() {
// JavaScript to be fired on all pages
},
finalize() {
// JavaScript to be fired on all pages, after page specific JS is fired
}
};
8 changes: 8 additions & 0 deletions assets/scripts/routes/Home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
init() {
// JavaScript to be fired on the home page
},
finalize() {
// JavaScript to be fired on the home page, after the init JS
}
};
10 changes: 5 additions & 5 deletions assets/scripts/util/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ import $ from 'jquery';
// The routing fires all common scripts, followed by the page specific scripts.
// Add additional events for more control over timing e.g. a finalize event
export default class Router {
constructor(namespace) {
this.namespace = namespace;
constructor(routes) {
this.routes = routes;
}

fire(func, funcname, args) {
funcname = (funcname === undefined) ? 'init' : funcname;
let fire = func !== '';
fire = fire && this.namespace[func];
fire = fire && typeof this.namespace[func][funcname] === 'function';
fire = fire && this.routes[func];
fire = fire && typeof this.routes[func][funcname] === 'function';

if (fire) {
this.namespace[func][funcname](args);
this.routes[func][funcname](args);
}
}

Expand Down

1 comment on commit f34af48

@tobeycodes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this

Please sign in to comment.