Skip to content

Instantly share code, notes, and snippets.

View mikeapr4's full-sized avatar

Michael Gallagher mikeapr4

  • Buenos Aires, Argentina
View GitHub Profile
actions: {
loadBooks({ commit }) {
commit('startLoading');
get('/api/books').then((response) => {
commit('setBooks', response.data.books);
commit('stopLoading');
});
},
loadAuthors({ commit }) {
commit('startLoading');
actions: {
loadBooks({ commit }) {
commit('startLoading');
get('/api/books').then((response) => {
commit('setBooks', response.data.books);
commit('stopLoading');
});
},
},
@mikeapr4
mikeapr4 / mutations-v-actions--first-look.js
Last active April 14, 2019 00:44
Code Samples - Mutations vs Actions
mutations: {
setName(state, name) {
state.name = name;
},
},
actions: {
setName({ commit }, name) {
commit('setName', name);
},
},
@mikeapr4
mikeapr4 / getter-reactive-caching.js
Last active May 29, 2019 08:22
example of reactive method-style getter caching
// recreation of a computed property
const computed = (vm, func) => {
vm.$watch(
func,
null, // callback not used for lazy
{ lazy: true }, // lazy means we can just flag as dirty
);
// eslint-disable-next-line no-underscore-dangle
const watcher = vm._watchers[vm._watchers.length - 1];
@mikeapr4
mikeapr4 / getter-cache.js
Last active March 10, 2019 09:48
an example rudimentary vuex method-style getter cache - don't use this, just for illustration
mappedTask2: (state, getters) => {
const cache = {};
return (id) => {
const record = state.tasks[id];
const key = id + record.name;
if (!cache[key]) {
cache[key] = getters.mappedTask(id);
}
return cache[key];
};
@mikeapr4
mikeapr4 / reactive-moment.js
Created August 2, 2018 11:56
Reactive-compatible Moment.js Instances
import moment from 'moment';
import 'moment-timezone';
// Make all fields on `object` hidden (not enumerable)
// except any in the whitelist.
const hideFields = (object, whitelist) => Object.keys(object)
.forEach((field) => {
if (!whitelist.includes(field)) {
const val = object[field];
Object.defineProperty(object, field, {
@mikeapr4
mikeapr4 / flatten.js
Created August 9, 2017 15:40
Flatten Nested Arrays in Javascript
// See this in action: https://jsfiddle.net/mikeapr4/t4x0oe9k/
// Queue-orientated solution, will work for large-scale
// nesting (50k+), however additional overhead of space (queue)
const flatten = A => {
const output = [], queue = [...A]
while (queue.length > 0) {
const entry = queue.shift()
if (Array.isArray(entry)) {
@mikeapr4
mikeapr4 / backbone-model-reactive.js
Last active June 16, 2017 14:28
Add reactive properties to a backbone model to allow plain Object manipulation.
var originalSet = Backbone.Model.prototype.set
, noConflictPrefix = '$$';
/**
* During initial construction, a model
* calls set(), but there is also a check
* to ensure the reactivity only occurs on construction
*/
Backbone.Model.prototype.set = function() {
var duringConstruction = (this.changed === null);
@mikeapr4
mikeapr4 / inquire.js
Created February 12, 2016 16:28
A mocking / injection mechanism for RequireJS
/******************************************************************************
* Inquire: a mocking mechanism for RequireJS (v2.1.9 - tightly coupled) *
* *
* Unversioned *
* *
* NOTE: Don't use Inquire for anything which modifies global scope! *
******************************************************************************/
var inquire;
(function(require) {
@mikeapr4
mikeapr4 / AjaxAwareAuthEntryPointWrapper.java
Created February 12, 2016 16:01
Wrapper for Spring Security AuthenticationEntryPoint interface, which will intercept 302 Redirects which are not supported generally in Browsers for Ajax Requests.
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.IOException;
public class AjaxAwareAuthEntryPointWrapper implements AuthenticationEntryPoint {