Skip to content

Instantly share code, notes, and snippets.

@subimage
subimage / gist:1255562
Created October 1, 2011 03:42
Sencha touch app file
//Ext.ns('CbMobile', 'CbMobile.views');
new Ext.Application({
name: 'CbMobile',
SIDEBAR_WIDTH: 320,
MENU_WIDTH: 250,
MENU_HEIGHT: 200,
launch: function() {
var app = this;
@subimage
subimage / is_defined.js
Created April 18, 2012 05:19
Javascript method to check if a chained object is defined.
/**
* Take string input in varName and determine whether it is defined object in Javascript
* ...like isDefined('foo.bar.baz');
* Better than doing if(foo && foo.bar && foo.bar.baz){}
* @param {String} varName
* @return {boolean}
* @author Bilal Soylu
*/
function isDefined(varName) {
var retStatus = false;
@subimage
subimage / array_chunk.js
Created April 19, 2012 06:50
Javascript array chunk
// Splits an array into equal sized chunks
Array.prototype.chunk = function(pieces) {
pieces = pieces || 2;
var len = this.length;
var mid = (len/pieces);
var chunks = [];
var start = 0;
for(var i=0;i<pieces;i++) {
var last = start+mid;
if (!len%pieces >= i) {
@subimage
subimage / backbone.basecollection.js
Created May 20, 2012 09:08
Backbone.js collection 'freshen' method that respects local changes.
Backbone.BaseCollection = Backbone.Collection.extend({
// Holds id of locally deleted items we ignore if we
// 'freshen' the collection and changes haven't propagated yet.
_localDeletedIds: [],
// Take an array of raw objects
// If the ID matches a model in the collection, set that model
// If the ID is not found in the collection, add it
// If a model in the collection is no longer available, remove it
//
// Keeps local changes, in case we've added things in the meantime.
@subimage
subimage / backbone.prototype.js
Created June 8, 2012 21:43
Backbone.js 0.9.1 that works with PROTOTYPE.js
// Backbone.js 0.9.1
// (c) 2010-2012 Jeremy Ashkenas, DocumentCloud Inc.
// Backbone may be freely distributed under the MIT license.
// For all details and documentation:
// http://backbonejs.org
//
// HACKED TO BE PROTOTYPE.JS COMPATIBLE BY [SUBIMAGE AT GMAIL DOT COM]
// REQUIRES PROTOTYPE >= 1.7.1
// !!! TODO needs tests!
@subimage
subimage / fuck_yourself_in_ie.js
Created June 28, 2012 20:48
How you can totally fuck yourself with JavaScript in IE.
// Inline assignment inside an 'if' statement works with most programming languages.
// However, in JavaScript there's a gotcha.
// This works in Safari, Firefox & Chrome...but will destroy your life in IE.
if(thing=functionThatReturnsThing()) {
doStuff();
}
// Do this instead
var thing=functionThatReturnsThing();
@subimage
subimage / gnarly_rails_scope.rb
Created July 6, 2012 04:53
Gnarly Rails scope of the day
# Projects a user has access to either through ProjectAssignment, or as an "account manager"
named_scope :user_has_access_to, lambda{|user|
{
:select => 'DISTINCT projects.*',
:joins => %q\
LEFT JOIN project_assignments
ON (
projects.id = project_assignments.project_id
AND project_assignments.has_access = 1
)
@subimage
subimage / .autotest.rb
Created July 8, 2012 20:45
Pass files or directories to autotest
# If you have a large test suite, being able to test only a subset
# of those items is a big speed boost.
#
# With this in your .autotest file you can run "autotest /some/dir" and only test
# the files within.
#
require 'redgreen/autotest'
Autotest.add_hook :initialize do |at|
unless ARGV.empty?
@subimage
subimage / CbMobile.model.Base.js
Created September 9, 2012 18:40
Sencha touch data model that fires CRUD events and can reload data from server
// Extended model that fires events on CRUD actions,
// and allows for reloading of data from the server.
Ext.define('CbMobile.model.Base', {
extend: 'Ext.data.Model',
// Overloaded methods that fire events
set: function(fieldName, value) {
this.callParent(arguments);
this.fireEvent('set', fieldName, value);
},
@subimage
subimage / ColorPickerField.js
Created September 11, 2012 08:39
Sencha Touch 2 custom colorpicker
/*
Original Author : Mitchell Simoens
ST2 revision Author : Di Peng
ST2 revised revision by: webmaster@subimage.com
Purpose : Creation of a custom color picker
License : GPL v3 (http://www.gnu.org/licenses/gpl.html)
Warranty : none
Price : free