Skip to content

Instantly share code, notes, and snippets.

View carlosrymer's full-sized avatar

Carlos Rymer carlosrymer

  • Shopify
  • New York, NY
View GitHub Profile
@carlosrymer
carlosrymer / ng-touch-ng-click-fix-mobile-links.js
Last active September 2, 2016 02:24
Fix event bubbling prevention issue with ngClick when using ngTouch on a mobile device
@carlosrymer
carlosrymer / ng-custom-style.js
Last active September 2, 2016 02:24
A custom directive that enhances ngStyle by observing style changes so that if a variable in an expression changes, it can update the style as well. This is useful when binding to a resource promise in styles.
app.directive("myStyle", function (){
return {
restrict: 'A',
link: function(scope, element, attrs)
{
var el = element[0],
attr = el.getAttribute('style');
el.setAttribute('style', attr);
@carlosrymer
carlosrymer / angular-resource-service.js
Last active September 2, 2016 02:23
Using ngResource to create Resource Service in AngularJS
(function() {
'use strict';
/**
* Resources service for interacting with the API
* @param {object} $resource
* @param {object} AppConfiguration
*/
function Resources($resource, AppConfiguration) {
@carlosrymer
carlosrymer / pageMetadata.js
Created October 8, 2014 02:05
Factory to Handle Page Metadata in an AngularJS App
(function() {
'use strict';
/**
* Page Metadata Factory
*/
function PageMetadataFactory() {
/**
* Metadata object
@carlosrymer
carlosrymer / DatabaseConnectionDisposer.js
Last active August 27, 2016 23:49
This is an example of how to use the disposer feature in Bluebird.js to immediately destroy a database connection once it's used.
// Let's assume that we're using the mysql module for Node.js in this example.
const mysql = require('mysql');
// We'll use Bluebird.js instead of the native Promise
const Promise = require('bluebird');
// Now, let's create a function that returns a promise that resolves to a database connection
function getConnection() {
return Promise.try(() => Promise.promisifyAll(mysql.createConnection({}))) // We're assuming default options here
.disposer(connection => connection.destroy()); // Disposer will be called once getConnection resolves
@carlosrymer
carlosrymer / pureFunctionsPromiseChain.js
Last active August 27, 2016 23:35
Keeping functions pure in a Promise chain
// One of the biggest challenges I've seen people face when using promises
// has to do with how to keep functions in a chain "pure". In other words,
// how can functions in a promise chain have only one output for a unique
// set of arguments, produce no side effects, and never rely on an external state?
// Let's start with an example of the problem
// Say we need to fire an http request to get a piece of information represented in json.
// We have a function named getInfo() that returns a promise that resolves with the info needed.
@carlosrymer
carlosrymer / BasicES6PromiseClass.js
Last active September 21, 2018 14:52
Simple ES6 Promise Class
// Simulating a Basic Promise in ES6
class Promise {
constructor(callback) {
this._errorCallbacks = [];
this._successCallbacks = [];
if (typeof callback === 'function') {
try {
callback(this.resolveIt.bind(this), this.rejectIt.bind(this));
} catch (e) {
@carlosrymer
carlosrymer / getGoogleSheetRows.js
Last active July 6, 2017 00:28
A Node snippet to get a Google Sheet's rows using the google-spreadsheet module.
'use strict';
const GoogleSpreadsheet = require('google-spreadsheet');
const spreadsheet = new GoogleSpreadsheet('1tVc-GnBIAAWbdCX7Zn9n-9V8j_rnv7meU22EubLN6G0'); // Sheet ID (visible in URL)
spreadsheet.getInfo((sheetError, info) => {
if (sheetError) {
console.error(sheetError);
@carlosrymer
carlosrymer / getGoogleSheetRowsLambda.js
Created July 6, 2017 00:28
A sample AWS Lambda function that retrieves rows from a Google Sheet.
'use strict';
const GoogleSpreadsheet = require('google-spreadsheet');
const spreadsheet = new GoogleSpreadsheet('1tVc-GnBIAAWbdCX7Zn9n-9V8j_rnv7meU22EubLN6G0'); // Sheet ID (visible in URL)
exports.handler = (event, context, callback) => {
return spreadsheet.getInfo((sheetError, info) => {
if (sheetError) {
console.error(sheetError);
@carlosrymer
carlosrymer / package.json
Last active July 30, 2017 15:08
A sample package.json for configuring an Angular 1.x app with Webpack 3.
{
"name": "myApp",
"version": "1.0.0",
"private": "true",
"scripts": {
"build": "./node_modules/.bin/webpack --config webpack.build.config.js",
"start": "./node_modules/.bin/webpack-dev-server --config webpack.dev.config.js --open"
},
"dependencies": {
"angular": "1.6.5",