Skip to content

Instantly share code, notes, and snippets.

View gaboelnuevo's full-sized avatar

Gabriel S. L gaboelnuevo

View GitHub Profile
@gaboelnuevo
gaboelnuevo / calculate-aspect-ratio.js
Created September 3, 2015 22:30
Calculate a ratio from two numbers
//calculate aspect ratio
// Via: http://stackoverflow.com/questions/1186414/whats-the-algorithm-to-calculate-aspect-ratio-i-need-an-output-like-43-169
function gcd (a, b) {
return (b == 0) ? a : gcd (b, a%b);
}
var w = screen.width;
var h = screen.height;
var r = gcd (w, h);
console.log("Dimensions = " + w + " x " + h);
@gaboelnuevo
gaboelnuevo / gist:7b48ea763a3665f0abfa
Created September 13, 2015 23:33
mixin: timestamp loopback
module.exports = function(Model, options) {
// Model is the model class
// options is an object containing the config properties from model definition
Model.defineProperty('created', {type: Date, default: '$now'});
Model.defineProperty('modified', {type: Date, default: '$now'});
}
@gaboelnuevo
gaboelnuevo / gist:35ed9e96e8194b103fad
Created September 13, 2015 23:35
loopback: Model filtered properties
var FILTERED_PROPERTIES = ['createdAt', 'updatedAt'];
Model.observe('before save', function filterProperties(ctx, next) {
if (ctx.options && ctx.options.skipPropertyFilter) return next();
if (ctx.instance) {
FILTERED_PROPERTIES.forEach(function(p) { ctx.instance.unsetAttribute(p); });
} else {
FILTERED_PROPERTIES.forEach(function(p) { delete ctx.data[p]; });
}
next();
});
@gaboelnuevo
gaboelnuevo / place.js
Created June 2, 2016 03:51
Loopback geopoint nerby place example
module.exports = function(Place) {
Place.nearby = function(here,cb){
Place.find({where: {location: {near: here}}, limit:3}, function(err, nearbyPlaces) {
cb(null, nearbyPlaces);
});
};
Place.remoteMethod('nearby', {
accepts: [
{arg: 'here', type: 'GeoPoint', required: true, description: 'geo location (lat & lng)'}
@gaboelnuevo
gaboelnuevo / Normalizer.js
Created July 5, 2016 14:39
Normalizr takes JSON and a schema and replaces nested entities with their IDs, gathering all entities in dictionaries.
// App/Services/Normalizer.js
import { Schema, arrayOf, normalize } from 'normalizr'
import { camelizeKeys } from 'humps'
const normalizer = (data, schema) => {
const camelizedJson = camelizeKeys(data)
return normalize(camelizedJson, schema)
}
@gaboelnuevo
gaboelnuevo / authentication.js
Created July 8, 2016 21:24
Loopback user literal
// server/boot/authentication.js
server.middleware('auth', loopback.token({
model: server.models.accessToken,
currentUserLiteral: 'me'
}));
@gaboelnuevo
gaboelnuevo / model.js
Created July 8, 2016 21:30
Loopback.io current user
// common/models/model.js
app.models.Model.beforeRemote('create', function(ctx, modelInstance, next) {
ctx.args.data.userId = ctx.req.accessToken.userId;
next();
});
@gaboelnuevo
gaboelnuevo / knockout-tags.js
Last active October 12, 2016 19:17 — forked from droyad/knockout-tags.js
Knockout binding for Tagit
// Based on https://gist.github.com/droyad/6136446
// Use with a observableArray.
// Example:
// <ul data-bind="tags: Tags"></ul>
ko.bindingHandlers.tags = {
init: function(element, valueAccessor, allBindingsAccessor) {
var bind = function() {
@gaboelnuevo
gaboelnuevo / Loopback facebook login
Created October 13, 2016 03:59
loopback.io: Facebook login with out cookie (only json)
npm install --save passport-facebook-token
Then add this entry in providers.json:
"facebook-mobile" : {
"provider": "facebook-token",
"module": "passport-facebook-token",
"strategy": "FacebookTokenStrategy",
"clientID": "XXXXXXX",
"clientSecret": "XXXXXXX",
"callbackPath": "/auth/facebook-token/callback",
var express = require('express');
var app = express();
var url = "https://username.jsreportonline.net/";
var client = require("jsreport-client")(url, "[email protected]", "password");
app.get('/', function (req, res) {
res.send('Go to /report or /base64');