Skip to content

Instantly share code, notes, and snippets.

View huafu's full-sized avatar
🏠
Writing code poems from home

Huafu Gandon huafu

🏠
Writing code poems from home
View GitHub Profile
@huafu
huafu / app.coffee
Created August 10, 2013 06:22
Ember router, routes, controllers and dependencies
App = Em.Application.create()
App.Router.map ->
@resource 'organizations', path: 'orgs', ->
@resource 'organization', path: ':organization_id', ->
@route 'index', path: '/'
@route 'settings'
# the route for all organizations
App.OrganizationsRoute = Em.Route.extend
@huafu
huafu / application.js
Last active December 23, 2015 02:19 — forked from anonymous/gist:6565738
EmBlog = Ember.Application.create({
LOG_TRANSITIONS: true
});
EmBlog.Comment = DS.Model.extend({
body: DS.attr('string'),
post: DS.belongsTo('post')
});
@huafu
huafu / ExampleItemsController.coffee
Last active December 25, 2015 13:39
Mixins and view to handle searchable array controller and infinite scroll list view
App = require 'app'
###*
Controller ExampleItemsController
@class ExampleItemsController
@namespace App
@extends Ember.ArrayController
###
module.exports = App.ExampleItemsController = Ember.ArrayController.extend App.Mixins.PagedSearchableArrayController,
@huafu
huafu / showTest.coffee
Created February 8, 2014 17:04
Example of usit CoffeeScript abstraction library for EmberJS testing with QUnit
###*
Integration test module my composite stats show
###
class Test.MyCompositeStatsShowTest extends IntegrationTestSuite
# URL to load for ALL our tests
url: '/'
# Ajax mock groups to be used
ajaxMockGroupNames: ['basic']
@huafu
huafu / force-utc-date.coffee
Last active August 29, 2015 13:57
Forces all dates in the browser to use UTC
# force dates to be UTC
__timezoneOffset = (new Date).getTimezoneOffset() * 60 * 1000
__dateWrapper = (original) ->
->
unless @__hacked
@__hacked = yes
@setTime @getTime() - __timezoneOffset
try
res = original.apply @, arguments
catch err
@huafu
huafu / isSame.coffee
Created September 28, 2014 07:57
Deeply compare 2 variables and returns true if they are the same, else false
isSame = (obj1, obj2) ->
if obj1 is obj2
yes
else if obj1? and obj2?
if (t = typeof obj1) isnt typeof obj2
no
else
if t isnt 'object'
no
else
@huafu
huafu / with-clock.js
Last active August 29, 2015 14:07
A mixin to have a property updated each time a new `tickStep` `tickUnit` is passed
import Ember from 'ember';
var DAYS_IN_YEAR = 365.25;
var DAYS_IN_MONTH = DAYS_IN_YEAR / 12;
var MILLISECONDS_IN = {
millisecond: 1,
second: 1000,
minute: 1000 * 60,
hour: 1000 * 60 * 60,
day: 1000 * 60 * 60 * 24,
@huafu
huafu / application.js
Created October 7, 2014 10:26
Application ficture adapter with queryFixtures implementing limits, order and filters (requires momentjs)
import Ember from 'ember';
import DS from 'ember-data';
function hasOwn(o, p) {
return {}.hasOwnProperty.call(o, p);
}
function castId(r) {
return '' + (r && r instanceof DS.Model ? r.get('id') : r);
}
@huafu
huafu / logger.coffee
Created October 21, 2014 15:46
Very simple logger to instrument objects and methods
logger =
_anonymousIndex: 0
guessMethodName: (method) ->
unless (res = (->).toString.call(method).match(/function([^\(]*)/)[1].replace(/(^\s+|\s+)$/g, ''))
res = logger.autoNamespace()
res
autoNamespace: ->
"[anonymous##{ ++logger._anonymousIndex }]"
@huafu
huafu / with-style.js
Created October 23, 2014 04:04
Add a `styleBindings` property to views which can be used like `attributeBindings`. Support units as well.
import Ember from 'ember';
/**
* @mixin WithStyleMixin
*/
var WithStyleMixin = Ember.Mixin.create({
concatenatedProperties: ['styleBindings'],
attributeBindings: ['style'],
/**