Skip to content

Instantly share code, notes, and snippets.

@craigmaslowski
craigmaslowski / backbone.error.js
Created August 28, 2013 18:58
backbone.error.js
Backbone.Error = function (message, obj) {
var type;
// TODO: Clean this up.
if (obj instanceof Backbone.Model) {
type = 'model';
} else if (obj instanceof Backbone.View) {
type = 'view';
} else if (obj instanceof Backbone.Collection) {
type = 'collection';
Backbone.mixin = function (obj, mixin) {
var source = obj.prototype || obj;
if (!mixin)
throw new Error('Backbone.mixin: The mixin specified is undefined.');
// merge all properties in the mixin into the view's prototype
_.defaults(source, mixin);
// merge object literal properties (ensure's events, bindings, and channels)
@craigmaslowski
craigmaslowski / euler11.js
Created June 6, 2013 20:33
Project Euler Problem #11
var grid = [
[08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 08],
[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00],
[81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65],
[52, 70, 95, 23, 04, 60, 11, 42, 69, 24, 68, 56, 01, 32, 56, 71, 37, 02, 36, 91],
[22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80],
[24, 47, 32, 60, 99, 03, 45, 02, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50],
[32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70],
[67, 26, 20, 68, 02, 62, 12, 20, 95, 63, 94, 39, 63, 08, 40, 91, 66, 49, 94, 21],
[24, 55, 58, 05, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72],
@craigmaslowski
craigmaslowski / sortBy.js
Created June 6, 2013 18:44
UNTESTED - Sort Backbone Collection by up to three terms.
sortBy: function (direction, primary, secondary, tertiary) {
this.models.sort(function (a, b) {
var aPrimary, aSecondary, aTertiary, bPrimary, bSecondary, bTertiary;
// setup return values according to the sort direction
var lessThan = (direction === 'asc') ? -1 : 1,
greaterThan = (direction === 'asc') ? 1 : -1;
aPrimary = a.get(primary);
bPrimary = b.get(primary);
@craigmaslowski
craigmaslowski / example.js
Last active December 15, 2015 13:09
Memoized module function with support for 'dot.notated.namespacing'
var m1 = module('modules.galore.yo');
m1.foo = 'bar';
var m2 = module('modules.galore')
console.log(m2); // { yo: { foo: 'bar' } }
Backbone.puree = function (config) {
if (config.all) {
config.view = config.view || [];
config.collection = config.collection || [];
config.model = config.model || [];
config.view = config.view.concat(config.all);
config.collection = config.collection.concat(config.all);
config.model = config.model.concat(config.all);
}
@craigmaslowski
craigmaslowski / iframe.js
Created December 28, 2012 23:47
Backbone View For Controlling an IFrame
IFrame = Backbone.View.extend({
initialize: function () {
_.bindAll(this);
},
navigate: function (route) {
this.$el.attr('src', window.location.href + route);
return this;
},
@craigmaslowski
craigmaslowski / bootstrap.modal.js
Last active October 13, 2015 15:38
Backbone Mixins
(function (Mixins) {
Mixins.BootStrapModal = {
show: function () {
var self = this;
if (self.preShow)
self.preShow();
// TODO - Review CSS to make it more generic
self.$el.modal().css({
@craigmaslowski
craigmaslowski / bootstrap.popover.js
Created November 28, 2012 21:08
Bootstrap Popover modified to show existing content (still needs some cleanup)
/* ===========================================================
* bootstrap-tooltip.js v2.2.1
* http://twitter.github.com/bootstrap/javascript.html#tooltips
* Inspired by the original jQuery.tipsy by Jason Frame
* ===========================================================
* Copyright 2012 Twitter, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@craigmaslowski
craigmaslowski / gist:1793576
Created February 10, 2012 22:26
Compound Id's with Backbone
parse: function (respo) {
respo.compoundId = respo.id + '::' + respo.type;
return respo;
},
sync: function (method, model, options) {
if (model.attributes.compoundId) {
model.id = model.get('id');
delete model.attributes.compoundId;
}