Skip to content

Instantly share code, notes, and snippets.

View AlanJenkinsVS's full-sized avatar

Alan Jenkins AlanJenkinsVS

View GitHub Profile
(function(window){
window.vs = window.vs || {};
window.vs.debounce = window.vs.debounce || debounce;
// Snatched debounce function from underscore.js
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
@AlanJenkinsVS
AlanJenkinsVS / SmarterWatchers.js
Created April 16, 2018 15:04
1. Smarter Watchers - Vue JS Optimisations
// Typical pattern
created() {
this.fetchUserList();
},
watch: {
searchText() {
this.fetchUserList();
}
}
@AlanJenkinsVS
AlanJenkinsVS / ComponentRegistration.js
Created April 16, 2018 15:16
2. Component Registration - Vue JS Optimisations
/*
* Loads components from this directory with the name format of 'base-{name}.vue'.
* Components are registered globally.
* Note - don't do this for every component as this will bloat your bundles
*/
import Vue from 'vue';
import upperFirst from 'lodash/upperFirst';
import camelCase from 'lodash/camelCase';
@AlanJenkinsVS
AlanJenkinsVS / index.js
Created April 16, 2018 15:24
3. Module Registration - Vue JS Optimisations
// Typical module registration attaches all modules immediately to the Vuex store
import auth from './modules/auth';
import posts from './modules/posts';
import comments from './modules/comments';
// ...
export default new Vuex.Store({
modules: {
auth,
posts,
@AlanJenkinsVS
AlanJenkinsVS / router_view.js
Created April 16, 2018 15:43
4. Cleaner Views - Vue JS Optimisation
/*
Add a key to router view. This tells Vue to re-render the view from scratch.
While this has negligibly worse performance from route to route it does simplify the
component as you no longer need to reset starting variables, etc within the component.
*/
<router-view :key="$route.fullPath"></router-path>
@AlanJenkinsVS
AlanJenkinsVS / App.vue
Created April 16, 2018 15:50
5. Transparent Wrappers - Vue JS Optimisations
<script>
export default {
inheritAttrs: false
}
</script>
<BaseInput placeholders="What's your name" @focus="doSomething" />
@AlanJenkinsVS
AlanJenkinsVS / App.vue
Created April 16, 2018 15:59
6. Single Root Components - Vue JS Optimisations
<template>
<ul>
<NavBarRoutes :routes="persistentNavRoutes />
<NavBarRoutes
v-if="loggedIn"
:routes="loggedInNavRoutes" />
<NavBarRoutes
v-else
@AlanJenkinsVS
AlanJenkinsVS / map.js
Created April 16, 2018 16:11
7. Rendering non-HTML - Vue JS Optimisation
const map = new mapboxgl.Map(/* ... */);
map.on('load', () => {
// Data
map.addSource(/* ... */);
// Layers
map.addLayer(/* ... */);
map.addLayer(/* ... */);
@AlanJenkinsVS
AlanJenkinsVS / results_per_page.amp.html
Created November 29, 2018 11:07
Using AMP to display a dropdown showing options for Results per Page
<!--
Expect back-end src to respond with a results_per_page array of objects
{
"items": {
"results_per_page" :[{
"value": "10",
"text": "10",
"selected": true
},{
<!--
Switch client-side view from 3 to 4 column grid
-->
<!-- Start Grid Size -->
<amp-bind-macro
id="activeButtonStyle"
arguments="attr, val"
expression="filtering[attr] == val ? 'p-1 border bg-black text-white cursor-pointer' : 'p-1 border cursor-pointer'"></amp-bind-macro>