Skip to content

Instantly share code, notes, and snippets.

@twalker
twalker / backbone-promises.js
Last active September 3, 2023 17:08
Monkey patch Backbone sync operations to return ES6 promises
/**
* backbone-promises returns ES6 Promises for Backbone.sync operations.
* @return {Object} monkey-patched Backbone.
*
*/
define(['backbone-lib', 'es6-promise'], function(Backbone, Promise){
// Backbone.ajax to return native ES6 promises for ajax calls insead of jQuery.Deferreds
Backbone.origAjax = Backbone.ajax;
Backbone.ajax = function ajax(){
/**
* edible adds contentEditable behavior to an element
*
* uses a subset of contenteditable API:
* https://developer.mozilla.org/en-US/docs/Rich-Text_Editing_in_Mozilla">content editable</a></p>
*
* inspired by
* https://github.com/mindmup/bootstrap-wysiwyg
* execCmd:
* https://developer.mozilla.org/en-US/docs/Rich-Text_Editing_in_Mozilla
@twalker
twalker / keyable.js
Last active September 20, 2015 21:22
Backbone View mixin to handle keyboard events.
/**
* View mixin to handle keyboard events.
* It requires patching for method overrides.
*
* @example
* var KeyedView = Backbone.View.extend({});
* mixer.patch(KeyedView.prototype, keyable);
*
* var keyedView = new KeyedView({
* keyboardEvents: {
/**
* ImageReady promises to inform when an image is loaded.
*
* uses es6-promises.
* inspired by: http://www.html5rocks.com/en/tutorials/es6/promises/
*
* @example
* ready(elImg).then(function(img){
* console.log('image is loaded');
* }, function(err){
@twalker
twalker / sublime-settings.json
Last active December 24, 2015 03:29
sublime settings
{
"folder_exclude_patterns": ["bower_components", "node_modules", "dist"],
"bold_folder_labels": false,
"caret_style": "wide",
"color_scheme": "Packages/Theme - Nil/Big Duo.tmTheme",
"colored_folder_glyphs": false,
"draw_white_space": "selection",
"ensure_newline_at_eof_on_save": true,
"fade_fold_buttons": false,
"font_size": 12,
@twalker
twalker / layout.js
Created September 12, 2013 04:30
A layout Backbone.view for assiging child views in data-regions. Probably the 1000th person to write something similar.
/**
* @module Layout is a composite view of child views
* which are assigned to regions defined in the Layout's template
* denoted with `data-region="keyname"` attributes.
* These attributes correspond with a region property with a subview reference.
* Views can be assigned at instantiation or after.
* Like a faithful parent, Layout cleans up after it's child views.
*
* TOREVISIT: layoutable as mixin
*
@twalker
twalker / filterable.js
Last active December 22, 2015 10:38
A filterable mixin for Backbone.Collection.
/**
* Filterable mixin for collections that need multiple filters applied simultaneously.
* Useful when the collection is in a grid with filter views.
*
* @example
* // require filterable and mix into the consuming collection.
* var MyCollection = Backbone.Collection.extend({
* // consuming collections are encouraged to expose custom filters.
* addTypeFilter: function(typeId){
* return this.addFilter('type', function(model){
@twalker
twalker / untabify.sh
Created August 10, 2013 21:58
untabify to 2 space indentation
find ./ -iname "*.js" -type f -exec sed -i 's/\t/ /g' {} \;
@twalker
twalker / synced-promise-test.js
Last active December 20, 2015 07:58
A Backbone.sync where deferreds use the same args as callbacks
require([
'mocha',
'chai',
'sinon',
'jquery',
'underscore',
'backbone',
'models/mixins/synced-promise'
], function(mocha, chai, sinon, jQuery, lodash, Backbone, sync){
@twalker
twalker / ready.js
Last active December 20, 2015 05:09
ready is Backbone view mixin to indicate when a view has rendered, returning a promise.
/**
* View.ready indicates when a view has rendered, returning a promise.
*
* Needs to be patched into a view's prototype due to the initialize and render methods.
* e.g. mixer.patch(View2.prototype, ready);
**/
define(function(require){
var jQuery = require('jquery');
return {