Skip to content

Instantly share code, notes, and snippets.

View jayphelps's full-sized avatar
💭
I may be slow to respond.

Jay Phelps jayphelps

💭
I may be slow to respond.
View GitHub Profile
@jayphelps
jayphelps / retry-with-replace.js
Created May 9, 2014 21:40
Common poorly documented Ember.js apis, tasks, conventions, etc. Some of these are private APIs! USE AT YOUR OWN RISK.
// Retry route Transition, but replace the URL state; just like router.replaceWith('route.name');
abortedTransition.retry().method('replace');
var parseLocation = (function () {
var a = document.createElement('a');
function parseLocation(url) {
a.href = url;
var protocol = (!a.protocol || a.protocol === ':') ? location.protocol : a.protocol;
// IE8 inconsistently returns leading slash
var pathname = (a.pathname.match(/^\//)) ? a.pathname : '/' + a.pathname;
@jayphelps
jayphelps / Object.resolve.js
Last active August 29, 2015 14:16
Resolve property paths
/**
Object.resolve(document, 'body.style')
*/
Object.resolve = function resolve(obj, propPath) {
return [obj]
.concat(propPath.split('.'))
.reduce((prevObj, currKey) => prevObj[currKey]);
};
@jayphelps
jayphelps / isValidEmail.js
Last active September 24, 2015 05:03
Validates an email, error on the side of letting bad ones through (best practice). Accepts emails with plus signs.
function isValidEmail(str) {
/**
* These comments use the following terms from RFC2822:
* local-part, domain, domain-literal and dot-atom.
* Does the address contain a local-part followed an @ followed by a domain?
* Note the use of lastIndexOf to find the last @ in the address
* since a valid email address may have a quoted @ in the local-part.
* Does the domain name have at least two parts, i.e. at least one dot,
* after the @? If not, is it a domain-literal?
*
@jayphelps
jayphelps / IdentifierBuilder.js
Created December 5, 2012 08:37
IdentifierBuilder - build the shortest possible unique names/keys. Helpful for compilers/minifiers/etc
/**
* MIT Licensed (but really, do what you want with it)
*
*
* Usage:
*
* `IdentifierBuilder.create()` === `a`
* `IdentifierBuilder.create()` === `b`
* `IdentifierBuilder.create()` === `c`
* ...
@jayphelps
jayphelps / newApply.js
Last active December 13, 2015 16:39
newApply() - A simple way to combine `foo.apply(context, args)` with `new Foo()` at the same time. (apply an array of arguments to the constructor of the new object instance)
/**
* newApply
* By Jay Phelps
* WTFPL Licensed
*
* Example:
*
* function Foo(first, second) {
* alert(first + ' ' + second);
* }
@jayphelps
jayphelps / index.html
Created July 19, 2013 05:50
Intro to Ember.js - Demo app from the Ember meetup presentation Slides: http://www.slideshare.net/jayphelps/intro-to-emberjs-24409989
<!DOCTYPE html>
<html>
<body>
<script type="text/x-handlebars">
<h1>My Great Web App</h1>
<div>
{{outlet}}
</div>
</script>
<script type="text/x-handlebars" data-template-name="index">
@jayphelps
jayphelps / app.js
Created January 15, 2014 06:38
Demo from my Ember.String.interpolate talk @ the Ember.js Southern California Meetup. Library: https://github.com/jayphelps/ember.string.interpolate Slides: https://www.slideshare.net/jayphelps/emberstringinterpolate
this.App = Ember.Application.create();
App.IndexController = Ember.ObjectController.extend({
firstName: null,
lastName: null,
fullName: '$firstName $lastName'.interpolate().readOnly()
});
var isValidURL = (function () {
var input = document.createElement('input');
input.type = 'url';
function isValidURL(url) {
input.value = url;
return input.validity.valid;
}
return isValidURL;
@jayphelps
jayphelps / history-scroll.js
Last active April 23, 2016 04:45
Ember router history Location class that keeps scroll position between transitions
var get = Ember.get;
var HistoryScrollLocation = Ember.HistoryLocation.extend({
document: document,
window: window,
_storeScrollPos: function () {
var state = history.state,
window = get(this, 'window'),
doc = get(this, 'document.documentElement');