Skip to content

Instantly share code, notes, and snippets.

View kpuputti's full-sized avatar

Kimmo Puputti kpuputti

View GitHub Profile
@kpuputti
kpuputti / gist:703944
Created November 17, 2010 19:51
compile errors
$ make test
g++ -std=c++98 -Wall -Wextra -g -D_GLIBCXX_DEBUG -Isample -lpthread -I.. -I../gtest -I../gtest/include ../gtest/src/gtest-all.cc gtest_main.a ../basic_functions/guest.cpp ../basic_functions/party.cpp ../basic_functions/table.cpp ../basic_functions/wish.cpp test_guests.cc test_tables.cc test_wishes.cc test_party.cc -o test
In file included from ../basic_functions/guest.h:15,
from ../basic_functions/party.h:13,
from ../basic_functions/guest.cpp:15:
../basic_functions/wish.h:102: error: expected ‘)’ before ‘&’ token
../basic_functions/wish.h:137: error: ISO C++ forbids declaration of ‘Guest’ with no type
../basic_functions/wish.h:137: error: expected ‘;’ before ‘&’ token
../basic_functions/wish.h:146: error: ‘Tablegroup’ was not declared in this scope
../basic_functions/wish.h:146: error: template argument 1 is invalid
@kpuputti
kpuputti / gist:1024838
Created June 14, 2011 13:00
console.log wrapper
var log = function () {
if (window.console && window.console.log && window.console.log.apply) {
var args = Array.prototype.slice.call(arguments);
args.unshift('My app:');
console.log.apply(console, args);
}
};
@kpuputti
kpuputti / gist:1029253
Created June 16, 2011 13:44
Javascript module pattern
var namespace = {};
namespace.module = (function () {
var priv = 'private var';
return {
init: function () {
// init module
},
@kpuputti
kpuputti / gist:1029264
Created June 16, 2011 13:52
Lazy initialization for a function
var lazy = (function () {
var initialized = false;
var init = function () {
// init code here
};
return function () {
if (!initialized) {
@kpuputti
kpuputti / gist:1040118
Created June 22, 2011 13:49
jQuery.getJSON abstraction to cache data to localStorage
var getCachedJSON = function (url, callback) {
var cachedData = window.localStorage[url];
if (cachedData) {
log('Data already cached, returning from cache:', url);
callback(JSON.parse(cachedData));
} else {
$.getJSON(url, function (data) {
log('Fetched data, saving to cache:', url);
window.localStorage[url] = JSON.stringify(data);
callback(data);
@kpuputti
kpuputti / gist:1056091
Created June 30, 2011 11:56
Asynchronous QUnit test with a mocked proxy for a caching json library
asyncTest('Basic fetch with an empty localStorage.', function () {
expect(7);
start();
var proxyMockCallCount = 0;
// Mock the json proxy to avoid networking.
JSONCache._getJSONProxy = function (url, options) {
proxyMockCallCount++;
if (url === 'data.json') {
options.success(testData);
@kpuputti
kpuputti / gist:1139644
Created August 11, 2011 13:27
Custon Javascript event trigger function
// http://www.html5rocks.com/en/mobile/workingoffthegrid.html
var fireEvent = function(name, data) {
var e = document.createEvent("Event");
e.initEvent(name, true, true);
e.data = data;
window.dispatchEvent(e);
};
@kpuputti
kpuputti / gist:1183277
Created August 31, 2011 10:41
Javascript references
var day = {name: 'day 1', rooms: [1, 2, 3]};
// create a separate var pointing to the same room list
var rooms = day.rooms;
day.rooms === rooms; // true
// delete the rooms list from the day object
delete day.rooms;
@kpuputti
kpuputti / gist:1338982
Created November 4, 2011 09:19
URL GEt params from location.hash parsing
// one liner
_.tap({}, function (o) { _.each(location.hash.split('&'), function (p) { var s = p.split('='); o[s[0]] = s[1]; }); });
// indented
_.tap({}, function (o) {
_.each(location.hash.split('&'), function (p) {
var s = p.split('=');
o[s[0]] = s[1];
@kpuputti
kpuputti / gist:1339460
Created November 4, 2011 14:34
touchstart handlers for backbone clicks
// In the events object of the view:
// 'touchstart nav a': 'onNavTouchStart',
// 'touchmove nav a': 'onNavTouchMove',
// 'touchend nav a': 'onNavTouchEnd',
onNavTouchStart: function (event) {
var href = $(event.target).attr('href');
this.navTouchHref = (href && href !== '#') ? href : null;
},