Skip to content

Instantly share code, notes, and snippets.

View arkitrave's full-sized avatar

Eric Shepherd arkitrave

View GitHub Profile
@arkitrave
arkitrave / tile-fit.js
Last active October 9, 2015 18:46
Will the tile fit? If you have a grid, but have some items which occupy multiple grid spaces, in a responsive layout, where can you place these larger tiles safely?
// Find out if a given tile can slot into a given grid position.
function willItFloat (opts) {
var
start = new Date().getTime(),
pos = opts.position, // The grid position requested (0-based)
size = opts.maxTileSpan, // The maximum number of tile "slots" this tile can take up
cols = opts.maxPageColumns, // The maximum number of columns the page can have at full width
arrays,
intersection;
@arkitrave
arkitrave / A functional JS secret santa generator
Last active October 9, 2015 18:45
Secret Santa generator
function secretSantaGenerator (num) {
var participants = _.shuffle(_.range(1, num));
return _.map(participants, function (val, index, list) {
return [
val,
list[index + 1] ? list[index + 1] : list[0]
];
});
}
foo = 'abc';
console.log(foo.length);
console.log(0.1+0.2);
//////////
var foo = {};
console.log('------ foo');
@arkitrave
arkitrave / template-blog-3.js
Created February 14, 2011 19:00
Step Three
Gilt.Template.render(
'baz', // The name of this feed or template type
data[0], // The data object to render
function(html) { // The callback function
Gilt.Notify.publish('feedTemplated', [html]);
},
{ version : 'bar' } // Options, in this case an alternate version
);
Gilt.Template.register('baz', '<div class="foo">' +
'<div class="foo-image">' +
'<img src="{{images/0}}" fullsrc="{{images/0}}" alt="{{{title}}}" />' +
'</div>' +
'<div class="foo-text">' +
'<h1>{{{brand_name}}}</h1>' +
'<h2>{{{title}}}</h2>' +
'</div>' +
'</div>',
{ version : 'bar' }
handlebars : {
cache: [],
render : function (name, template, content, context, callback) {
try {
if (!cache[name]) {
cache[name] = Handlebars.compile(template);
}
callback(cache[name](content, context));
} catch (e) {
// console.log('Could not render. Handlebars might not be installed');
@arkitrave
arkitrave / template-compare.js
Created February 14, 2011 18:51
Comparison of templated vs. non-templated HTML rendering
// Rendering markup with loops and string concatenation
for (var i = 0; i < foo.name.length; i++) {
content += '<li><a href="foo/' + foo.id[i] + '">' + foo.name[i] + '</a></li>';
}
// Rendering with a JavaScript template
content = '{{#foo}}' +
'<li><a href="foo/${id}">${name}</a></li>' +
'{{/foo}}';
// Gets the location in a grid of objects by numbered row and lettered column
// Example: 5C is the 5th row, 3rd column
getGridLocation = function($el) {
var $items = $el.find('.item:visible'),
index = $items.index($el),
lefts = [],
tops = [];
$items.each(function() {
var $item = $(this),
Date.prototype.beginningOfWeek = function () {
return new Date(this - ((this.getDay() - (arguments[0] || 0)) * 86400000));
}
@arkitrave
arkitrave / backbone-view-private-methods.js
Created July 1, 2015 16:26
Private methods in Backbone views
define('my_view',
[jquery, underscore, backbone],
function ($, _, Backbone) {
// These will all have "this" bound correctly
var
MyView,
helpers;