Skip to content

Instantly share code, notes, and snippets.

View StevenLangbroek's full-sized avatar

Steven Langbroek StevenLangbroek

View GitHub Profile
@StevenLangbroek
StevenLangbroek / class.js
Last active May 3, 2019 20:05
Private variables in ES6 Classes.
/**
* So, obviously this is by no means a "new trick",
* but encapsulation in classes works the same as
* it does in "Modules" (IIFE pattern): You hide them
* in a scope, and define the methods that need access
* to them within that same scope. The constructor is
* as good a place as any for that.
*/
import _ from 'underscore';
@StevenLangbroek
StevenLangbroek / flightplan.js
Last active August 29, 2015 14:11
Sample Flightplan for Deployments + Rollbacks
var plan = require('flightplan');
var moment = require('moment');
var currentTime = new Date().getTime();
var formattedCurrentTime = moment(currentTime).format('YYYY-MM-DD_HH-mm');
var backupsFilename = 'backups.json';
plan.target('staging', {
host: 'staging.some-host.com',
username: 'root-level-user',
describe "Saving", ->
before ->
@syncSpy = sinon.spy()
@syncStub = sinon.stub(Backbone, 'sync', @syncSpy)
@changeSpy = sinon.spy()
afterEach ->
@syncSpy.reset()
it 'sets errors on the model when trying to save', ->
@StevenLangbroek
StevenLangbroek / model.js.coffee
Last active November 17, 2018 10:09
Fetch a model with options unless it has an attribute. Return promise and resolve with instance so you can easily feed it further into your controllers.
class Model extends Backbone.Model
fetchUnless: (key, options = {}) ->
defer = $.Deferred()
if @has(key)
defer.resolve(@)
else
@fetch(options).done =>
defer.resolve(@)
@StevenLangbroek
StevenLangbroek / views.coffee
Created March 27, 2014 13:02
Given this code, how can I still add templateHelpers to individual views?
@AnalyticsApp.module "Views", (Views, App, Backbone, Marionette, $, _) ->
_.extend Marionette.View::,
templateHelpers: ->
form:
App.request "get:form"
i18n:
App.request "get:i18n"
// Generated on 2014-03-05 using generator-webapp 0.4.7
'use strict';
// # Globbing
// for performance reasons we're only matching one level down:
// 'test/spec/**/*.js'
// use this if you want to recursively match all subfolders:
// 'test/spec/**/*.js'
module.exports = function (grunt) {
@StevenLangbroek
StevenLangbroek / template.html
Last active August 29, 2015 13:56
TemplateHelpers
<script type="text/handlebars" id="my_list_template">
<div class="{{whichuser}}">
<img src="{{ image }}">
<div class="message_body">
{{{message_body}}}
</div>
</div>
</script>
@StevenLangbroek
StevenLangbroek / app.js
Last active August 29, 2015 13:56
Live filtering with Marionette in 39 lines of JS.
var M = Backbone.Marionette;
var App = new M.Application();
var ListItem = M.ItemView.extend({
tagName: 'li',
template: _.template('<h4 class="question"><%= question %></h4><p style="display: none;"><%= answer %></p>')
});
var List = M.CollectionView.extend({
@StevenLangbroek
StevenLangbroek / renderer.coffee
Created January 29, 2014 23:21
Marionette Renderer override to use `grunt-contrib-handlebars`
_.extend Marionette.Renderer,
render: (template, data) ->
throw "Template #{template} not found!" unless JST[template]
JST[template](data)
@StevenLangbroek
StevenLangbroek / 01_map_app.coffee
Last active January 3, 2016 03:29
Maps for MarionetteJS (not production ready, will probably refactor a couple of times)
@App.module 'MapApp', (MapApp, App, Backbone, Marionette, $, _) ->
@startWithParent = false
API =
showMap: ->
new MapApp.List.Controller
@on 'start', ->
API.showMap()