Skip to content

Instantly share code, notes, and snippets.

@jboesch
jboesch / gist:641505
Created October 22, 2010 22:35
requireJS dependencies
// Using http://requirejs.org/
// loading scripts/misc/person.js
require(["misc/person"], function(person) {
person.loaded(function(){
alert('dependencies loaded!');
// dependencies loaded, now do something with person
});
});
// Inside scripts/misc/person.js
@jboesch
jboesch / gist:641350
Created October 22, 2010 20:54
YUI example for loading dependencies
// Our modules.js file
var MyApp = MyApp || {};
MyApp.Modules = {
common: {},
css: {
'farbtastic.css': { type: 'css', varName: 'farbtastic.css', path: 'vendor/farbtastic/farbtastic.css' },
'jquery.gritters.css': { type: 'css', varName: 'jquery.gritters.css', path: 'vendor/jquery.gritter/jquery.gritter.css' },
'jquery.uploadify.css': { type: 'css', varName: 'jquery.uploadify.css', path: 'vendor/uploadify/uploadify.css' },
'jquery.daterangepicker.css': { type: 'css', varName: 'jquery.daterangepicker.css', path: 'vendor/jquery.daterangepicker/ui.daterangepicker.css'}
},
@jboesch
jboesch / gist:614548
Created October 7, 2010 04:22
django workaround for jquery tmpl tags
<script id="person-tmpl-django" class="fix" type="text/x-jquery-tmpl">
<div id="person-${user.id}" class="person">
<strong>${user.name}</strong>
[[each(i, c) user.children]]
<div class="child">${c.child_name}</div>
[[/each]]
<span>${user.age}</span>
</div>
@jboesch
jboesch / attachClass_init_self.js
Created September 28, 2010 18:47
attachClass init and self
Object.prototype.attachClass = function(name, o){
this[name] = function(){
if(typeof(this.__init) == 'function'){
this.__init.apply(this, arguments);
}
};
var new_methods = {};
for(var m in o){
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Mustache JS - Template in script block instead of js variable</title>
<script type="text/javascript" src="http://github.com/janl/mustache.js/raw/master/mustache.js"></script>
</head>
<body>
// This is just an example of a quick way of setting up a new instance with an automatic call to "init"
//
// Create our attachClass method
Object.prototype.attachClass = function(name, o){
this[name] = function(){
if(typeof(this.__init) == 'function'){
this.__init.apply(this, arguments);
}
}
this[name].prototype = o;
/*
Have you ever used Graphael Pie chart (http://g.raphaeljs.com/) and found that when you use a really long legend label, it sometimes goes right off the screen and your chart doesn't align with it? This fixes that.
It calculates the graph x to center align the labels with the chart. This only works if you position the pie chart 'south' or 'north' in the 'legendpos' option.
*/
var legend = ['Awesome', 'Legend', 'Labels whoa this is long'];
legend.sort(function(a, b){ return a.length < b.length; });
var longest_word = legend[0].length;
var x_offset = 1.5; // This might be different in your case. Mess around with it.
(function($){
window.App = {
// Site wide options
opts = {
rootURL: '/'
};
// Pass in any options to 'o' and merge it with global defaults
var Template = {
// Keep track of used selector text to prevent re-quering the DOM
_cache: {},
/**
* If you've already called this.draw() on a certain template and don't want to lose your data, you
* can append some more data by calling append()
* @param {String} tpl The template string that you created with draw();
* @param {Object} data The data you're passing
/**
* Allow for some super easy templating
* @param {Object} ctx The context when you pass in an object literal
* ------------------------------------------------------------------
* Works like this:
* var People = { name: 'bob', age: 123 }
* var my_tpl = "Hi, my name is {{ name }} and I'm {{ age }} years old."
* my_tpl.template(People); // Outputs: "Hi, my name is bob and I'm 123 years old";
*/
String.prototype.template = function(data){