Skip to content

Instantly share code, notes, and snippets.

View garth's full-sized avatar

Garth Williams garth

View GitHub Profile
@garth
garth / Assetfile.rb
Created July 17, 2012 12:56 — forked from wagenet/Assetfile.rb
Ember Handlebars Precompile
require 'execjs'
class HandlebarsFilter < Filter
class << self
def contents
@@contents ||= [File.read("headless-ember.js"), File.read("ember.js")].join("\n")
end
def context
@@context ||= ExecJS.compile(contents)
@garth
garth / gist:3966591
Created October 27, 2012 22:05
Pre-compile emberjs handlebars templates with browserify
var path = require('path')
var vm = require('vm')
var handlebarsjs = fs.readFileSync(__dirname + '/vendor/handlebars.js', 'utf8')
var emberjs = fs.readFileSync(__dirname + '/vendor/ember/ember.js', 'utf8')
browserify.register('handlebars', function (body, file) {
//dummy jQuery
var jQuery = function () { return jQuery }
jQuery.ready = function () { return jQuery }
jQuery.inArray = function () { return jQuery }
@garth
garth / BundleConfig.cs
Created November 8, 2012 14:40
Pre-compile emberjs handlebars templates with ASP.NET MVC Bundles
// use NuGet to add BundleTransformer to your project
// add ember.js and handlebars.js to the /Scripts folder
// in bundle config add something like this
bundles.Add(new ScriptBundle("~/bundles/templates").IncludeDirectory(
"~/templates", "*.handlebars"));
// or like this
bundles.Add(new ScriptBundle("~/bundles/teamplates").Include(
"~/templates/application.handlebars",
@garth
garth / gist:4056917
Created November 12, 2012 00:28
Authorise middleware for use with passportjs or similar
//
// check that the user has the correct role for access to the route.
// to use, ensure that user has a roles array then add the following to routes
//
// app.get('/admin-route', app.authorise('admin', 'super-admin'), function (req, res) { ... })
//
app.authorise = app.authorize = function () {
var roles = Array.prototype.slice.call(arguments)
return function (req, res, next) {
if (req.user && _.any(roles, function (role) {
@garth
garth / snockets-debug.coffee
Created November 22, 2012 19:49
Use snockets to package javascript files for debug output (with //@ sourceURL)
#entry point for js/coffee dependency tree
rootfile = 'root.js'
#get the file names and content
files = snockets.getCompiledChain rootfile, { async: false }
#package each file in eval with trailing //@ sourceURL
scripts = []
for file in files
script = file.js.replace(/\\/g, '\\\\').replace(/\n/g, '\\n').replace(/'/g, '\\\'')
@garth
garth / clientAccess-lookup.js
Created November 30, 2012 20:44
Using Emberjs code on the server side with Nodejs
// enable emberjs globally
GLOBAL.Ember = { lookup: GLOBAL }
require('./vendor/ember/ember-runtime')
// define the client App object
GLOBAL.App = Ember.Application.create({})
// include ember models
require('./public/js/login')
@garth
garth / app.js
Created December 19, 2012 09:45
Packaging javascript with asset-rack example code
window.App = Ember.Application.create({})
require('./applicationController')
@garth
garth / data_access.js
Created September 13, 2013 08:15
I had lots of problems getting ember data (1.0.0-beta.2) to work our back end services. After many hours of debugging I decided to write this hold us over until ember data stabilizes.
App.DataAccess = Ember.Object.create({
namespace: 'api',
cache: {},
getTypeName: function (type) {
return type.toString().match(/\w+$/);
},
isId: function (query) {
return typeof query === 'string' || typeof query == 'number';
@garth
garth / _input_text.hbs
Last active February 16, 2017 19:56
Example code for emberjs form validation. Working example can be seen here http://jsbin.com/ifARet/4
<label class="control-label"
{{bind-attr for="view.inputField.elementId"}}>
{{view.label}}
</label>
{{input
typeBinding="view.type"
class="form-control"
placeholderBinding="view.placeholder"
valueBinding="view.value"
viewName="inputField"}}
@garth
garth / runner.js
Created October 9, 2013 14:01
A PhantomJS test runner for qunit that includes a built in web server to host the JavaScript files and auto generate test.html This is especially useful when working in complex environment where running up a full web server during your automated build process might be difficult, since PhantomJS hosts and executes the tests without any external d…
var system = require('system');
var fs = require('fs');
if (system.args.length !== 2) {
console.log('Usage: runner.js website-base-path');
phantom.exit(1);
}
var basePath = system.args[1];
var outputFile = basePath + '/test.html';