Skip to content

Instantly share code, notes, and snippets.

View ksol's full-sized avatar

Kevin Soltysiak ksol

View GitHub Profile
@ksol
ksol / gist:e0424d2721674913c243
Last active August 29, 2015 14:04
Go: wait for n parallel tasks to complete
package main
import (
"fmt"
"sync"
"time"
)
func process(item string, index int, wg *sync.WaitGroup) {
fmt.Printf("Hitting endpoint #%d: %+v\n", index, item)
@ksol
ksol / controller.js
Created December 12, 2014 15:19
Ember.js: cancellable actions
import Ember from 'ember';
export default Ember.Controller.extend({
actionRunId: null, // This property stores the task id for our action
timerRunId: null, // this property stores the task id for the auto-updating of the timer
currentWaitTime: 0, // The timer (how many seconds are left until the action is executed)
waitTime: 5, // The delay (in seconds) during which you can cancel an action
cancelable: false, // Is there an action to cancel ?
cancelMessage: null, // The message to display to the user
@ksol
ksol / posts.json
Created December 14, 2014 20:47
API example
{
"posts":[
{
"id":1,
"title":"first-post"
},
{
"id":2,
"title":"second-post"
}
import Ember from 'ember';
export default Ember.Route.extend({
// Here, we're telling ember to "watch" the query parameter "page".
// In this case, if the parameter changes, the model will be fetched again.
// We could map the parameter to a different property name if we wanted.
queryParams: {
page: {
refreshModel: true
}
import Ember from 'ember';
export default Ember.Route.extend({
model: function() {
return this.get('store').find('post'));
},
setupController: function(controller, model) {
this._super.apply(this, arguments); // Do not forget this call
controller.set('totalPages', model.get('meta.totalPages'));
Ember.get({foo: {bar: 'hello'}}, 'foo.bar')
// <- "hello"
Ember.get({foo: {bar: 'hello'}}, 'nonexistent')
// <- undefined
var a = null;
var b; // undefined
a.get('myprop')
// Uncaught TypeError: Cannot read property 'get' of null
Ember.get(a, 'myprop');
// <- undefined
Ember.get(b, 'myprop');
// Do not do this
var SomeController = Ember.ArrayController.extend({
init: function() {
this._super.apply(this, arguments) // easy to forget, can introduce typos, etc
// your code here
}
});
// Do this
@ksol
ksol / chrome_39.js
Last active July 10, 2021 13:37
Language detection in javascript
window.navigator.language // -> "fr"
window.navigator.languages // -> ["fr-FR", "fr", "en-US", "en", "es", "de"]
window.navigator.userLanguage // -> undefined
window.navigator.browserLanguage // -> undefined
window.navigator.systemLanguage // -> undefined
@ksol
ksol / gist:d1b9481c6ea8bcc9828f
Created January 23, 2015 13:06
Javascript one-liner for finding user language
var langage = (navigator.language || navigator.browserLanguage).split('-')[0];