Skip to content

Instantly share code, notes, and snippets.

View foxnewsnetwork's full-sized avatar

Thomas Chen foxnewsnetwork

View GitHub Profile
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
import Ember from 'ember';
export default Ember.Controller.extend({
});
import Ember from 'ember';
export default Ember.Component.extend({
});
@foxnewsnetwork
foxnewsnetwork / controllers.application.js
Created August 11, 2016 21:23
Classifying unicode strings
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
title: Ember.String.classify('asdf 344 日本語')
});
@foxnewsnetwork
foxnewsnetwork / exposition.markdown
Created May 13, 2016 22:47
Ember.js handlebars template named yields

What is it?

Here is the Ember.js usage pattern for implementing named yield sections using nothing more than if statements and regular yield.

This has the advantage of not introducing irritating extra wrapper elements as is the case when using ember-wormhole or ember-named-yield

@foxnewsnetwork
foxnewsnetwork / exposition.markdown
Created January 20, 2016 00:22
ember component modifying properties in didInsertElement hook

The Problem

Starting sometime in Ember 1.13.x, the core team decided that it shall be a bad idea to modify component variables inside the didInsertElement hook in Ember components. The explanation they gave was that it degrades performance (and who knows, perhaps it does, but I never notice because the 1000+ms waits for remote server to return data).

Nevertheless, modifying component properties on didInsertElement is often a necessity when (for example) we need to know the rendered dimensions of an element in order to perform some action:

didInsertElement: ->
  width = @$().width()
  @set "containerWidth", width
 @set "itemWidth", width
@foxnewsnetwork
foxnewsnetwork / rant.markdown
Created January 13, 2016 18:46
Browser Set-Cookie XHR put request

Apparently xhr put (and patch) requests from browsers to remote servers ignore the response's Set-Cookie header. I don't know why browsers do this shit, but it's annoying as hell.

Remote Address:127.0.0.1:4000
Request URL:http://localhost:4000/api/sessions
Request Method:POST
Status Code:201 Created
Response Headers
view source
access-control-allow-credentials:true
access-control-allow-origin:http://localhost:4200
@foxnewsnetwork
foxnewsnetwork / exposition.markdown
Created September 5, 2015 23:54
Elixir application module attributes production loading

The setup

In my Elixir phoenix web app, I have a plug which handles requests made to an internal endpoint which forbids access to all users without a proper authorization in the request header.

Sounds simple right? Well it is, the below is my plug. It checks if the incoming request has a "simwms-master-key" field, and passes all users who have that key.

@foxnewsnetwork
foxnewsnetwork / query.ex
Created August 5, 2015 01:57
Postgres order_by select count column missing group_by
bad_query = from a in Appointment,
where: is_nil(a.deleted_at),
order_by: [desc: a.expected_at],
select: count(a.id)
good_query = from a in Appointment,
where: is_nil(a.deleted_at),
select: count(a.id)
Repo.one bad_query # throws ** (Postgrex.Error) ERROR (grouping_error): column "a0.expected_at" must appear in the GROUP BY clause or be used in an aggregate function
@foxnewsnetwork
foxnewsnetwork / exposition.markdown
Last active August 29, 2015 14:21
Complex Front End Data Models in Emberjs

The Motivation

EmberJS is a full-featured front-end javascript framework for building (as they put it) ambitious apps. Being ambitious, I wanted to build an app that had data models that were queryable like those of SQL, realtime like it was on Websockets, I wanted to do it without building my own customized REST-over-websockets-on-Elixir server stack. Why would I want to do this? Mostly because so many cuts on the server-side reduces devops costs (to 0) and allows me to piggyback on the extremely well-staffed architecture that runs places like Github and Google (so I don't have to ever worry computer-level details like disk IO rate, server RAM, database backup, SSL, geographic distribution, etc.; all of that I would get for free).

TL;DR: so I can build a globally scalable app that I can maintain just by myself (because there is no maintainence)

The Problem

Using Javascript to do heavy client-side data-management means I am either condemned to Calback Hell or (since it's 2015), Promise Monad Hell (y