Skip to content

Instantly share code, notes, and snippets.

View craigspaeth's full-sized avatar

Craig Spaeth craigspaeth

View GitHub Profile
@craigspaeth
craigspaeth / gist:1087868
Created July 17, 2011 17:56
Coffescript override Backbone.sync to be Ruby on Rails friendly
# Override Backbone.sync to be rails friendly
Backbone.sync = ((original) => (method, model, options) ->
options ?= {}
# Wrap model's attrs in a Rails friendly way by injecting it into options.data
if not options.data? and model? and (method is "create" or method is "update" or method is "delete")
unless model.modelName?
throw new Error "You must specify a modelName in a Rails friendly
underscore format for this model to be persisted"
@craigspaeth
craigspaeth / gist:1192424
Created September 4, 2011 06:59
Simple Jasmine spec helper to run an entire suite asynchronously
# Example usage:
#
# describe 'foo' ->
# runAsync()
# it 'tests things' ->
# expect(0 is 0).toBeTruthy()
# done()
#
_done = false
done = -> _done = true
@craigspaeth
craigspaeth / nodes_backbones_zombies.md
Created January 25, 2012 21:53
Nodes and Backbones and Zombies, oh my!

Javascript

Some examples to be run inside the node console.

Functional, closures, lambdas

// First class players
var fn = function () {
@craigspaeth
craigspaeth / gist:2512046
Created April 27, 2012 19:25
Mixin Art.sy uses to manage "frames" in our CMS app
#
# Mixin to a router to wrap `route` so that it clears ajax requests and transitions frames.
#
Backbone.FrameManager =
#
# Declare regex: frame pairs. If the route matches the regex it will create an instance
# of the frame once, and activate/deactivate that view from there on.
#
# A "frame" is a high-level view associated with an element that is a child of the #frames div.
@craigspaeth
craigspaeth / gist:3057320
Last active October 6, 2015 21:38
Lightweight Backbone Relations Mixin
#
# Wraps `get` and `set` to wrap raw data with instances of models. Also wraps `toJSON`
# to give better serialized output.
# mixin with _.extend @prototype, Backbone.Relations
#
# e.g.
#
# class Paws extends Backbone.Collection
# class Dog extends Backbone.Model
# _.extend @prototype, Backbone.Relations
info: Welcome to Nodejitsu craigspaeth
info: jitsu v0.9.7
info: It worked if it ends with Nodejitsu ok
info: Executing command restart
info: Restarting app juliacolavita.com
error: Error running command restart
error: Nodejitsu Error (500): Internal Server Error
warn: Error returned from Nodejitsu
error: Error: Application has not been started yet.
error: at module.exports (/root/nodejitsu/lib/nodejitsu/resources/app/controller/restart.js:26:23)
{
"name": "juliacolavita.com",
"subdomain": "juliacolavita",
"domains": [
"juliacolavita.com",
"www.juliacolavita.com"
],
"version": "0.0.1-23",
"private": true,
"engines": {
layout title date comments categories
post
Writing Headless Backbone Tests With Node.js
2013-06-14 17:48
true
Javascript
Backbone.js
Node.js

TL;DR

Relations:

I've tried Backbone relational before and hadn't had a great experience with it. Although that was a long time ago, and it may be robust enough to make sense now. Regardless, personally I'm not even sure built in relations have a place as a Backbone library (I even remember a talk by jashkenas where he briefly mentions not supporting relations/nested models in Backbone because Backbone core peeps though it was a bad idea). My experience tends to agree because API conventions can vary quite a-lot, even just in our own API. There tend to be more edge cases to handle than makes it worth saving a couple lines of code here an there.

I had attempted to write my own relational library for Backbone back in the day. But ultimately decided to drop it because of this exact point, it felt like a leaky abstraction. Instead I opted for a light-weight mixin for relations that Torque/Inertia uses. Although that has mostly be

@craigspaeth
craigspaeth / gist:6630959
Created September 19, 2013 22:52
A sample of a larger coming blog post about how Artsy shared code client and server for their mobile web app.

A simpler way of sharing code between server & client

Stop abstracting away the server/client environment

Frameworks like Rendr, Derby, and Meteor try to create this magical environment where you can stop thinking about whether your code is running on the client or server. This seems to lead to learning a bunch of concepts unique to the framework that define this "shared" environment. Sometimes that can feel like a leaky abstraction (what is a server-side Backbone view?), or lock you in to the full stack to make it work (Derby/Meteor backed by a REST API instead of mongo?).

Instead of tackling the massive problem of trying to abstract away the server and client, lets acknowledge they're completely different and embrace using modules of code that we can reasonably expect to work on both sides.

Some things that can run on both sides