Skip to content

Instantly share code, notes, and snippets.

@zhannes
zhannes / gist:4259920
Created December 11, 2012 16:17
js inheritance example
/* for commented version, see https://gist.github.com/4259920/95d6d741d227c274be1ecf5238998c01f3658131 */
function object(o){
function ctor(){}
ctor.prototype = o;
return new ctor;
}
function inheritProto(subType,superType){
var prototype = object(superType.prototype);
prototype.constructor = subType;
@zhannes
zhannes / mustache-helper-template.html
Created January 7, 2013 16:12
mustache.js helper functions - Add a property on your data object. Use the property name to reference the helper within your template. Inside the helper, you can reference the current object as `this`.
<script>
var people = getData(); // however you get your data ...
/* structure
{
people : [
{ name : "Shiva Khomini Somar Kondar Krohm" },
{ name : ... }
@zhannes
zhannes / this.js
Last active December 10, 2015 18:58
`this` in javascript, scoping, execution context
/* how to bind `this` for callbacks, good example here too:
http://ejohn.org/apps/learn/#84
*/
var uiThing = {
parseResponse : function(data){
this.data = data;
this.renderUI()
},
renderUI : function(){ /* more code */ }
@zhannes
zhannes / dfd.js
Last active December 11, 2015 23:08
// assumes Q.js has been loaded and available as Q
function ajaxCall(){
var dfd = Q.defer(),
data = getData({
success: function(response){
dfd.resolve(response);
},
error: function(error){
dfd.reject(error);
}
var mod = (function(){
var foo; // set from outside
function setItem(val){
val && (foo = val);
}
return { setItem: setItem };
@zhannes
zhannes / app.js
Last active December 12, 2015 04:38
rough touch based slider
(function($){
var slides, numSlides,
ct = 0,
settings = {
duration: 500,
easing: 'cubic-bezier(1.000, 0.000, 0.000, 1.000)'
};
function transition(){
@zhannes
zhannes / index.html
Last active December 12, 2015 10:38
avoid globals, use a namespace
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
</head>
<body>
<script src="js/module-1.js"></script>
<script src="js/module-2.js"></script>
</body>
@zhannes
zhannes / template-helper.js
Created April 13, 2013 21:46
Fetch a javascript template (html file) and then pass it data.
define(['jquery', 'handlebars'],function($,Handlebars){
/* use:
var tmpl = App.helpers.tmpl,
getCompiledTemplate = tmpl('foo', {});
getCompiledTemplate.then(function(content){
$('#something').html(content);
});
@zhannes
zhannes / Gemfile
Last active December 17, 2015 05:49
This is here now: https://github.com/zhannes/mm_starter. Middleman with slim, stylus, coffee script, assets (concat, minify, fingerprint)
# If you have OpenSSL installed, we recommend updating
# the following line to use "https"
source 'http://rubygems.org'
gem "middleman", :github => "middleman/middleman"
gem 'middleman-sprockets', :github => "middleman/middleman-sprockets"
gem "stylus"
gem "slim"
gem "gist"
(function($){
$(function(){
// code in here
})
})(window.jQuery)