Skip to content

Instantly share code, notes, and snippets.

View unscriptable's full-sized avatar
🏔️

John Hann unscriptable

🏔️
View GitHub Profile
@unscriptable
unscriptable / Stateful.js
Created December 24, 2010 05:05
cujo.Stateful
/*
cujo.Stateful
(c) copyright 2010, unscriptable.com
author: john
LICENSE: see the LICENSE.txt file. If file is missing, this file is subject to the AFL 3.0
license at the following url: http://www.opensource.org/licenses/afl-3.0.php.
Adds dojo.Stateful behavior to objects without the dojo.declare turds and overhead
@unscriptable
unscriptable / js-to-java.js
Created December 20, 2010 20:28
How to communicate between javascript and java applet
var myApplet = funcToGetApplet();
myApplet.setCallback({
myMethod: function (args) {
// do something with args here
},
anotherMethod: function (args) {
// do something with args here
@unscriptable
unscriptable / arbitrary_constructor.js
Created December 17, 2010 17:47
construct an object given a constructor and an array of parameters
function createObject (ctor, args) {
function F () {
ctor.apply(this, args);
}
F.prototype = ctor.prototype;
F.prototype.constructor = ctor; // IE might need this
return new F();
}
// quick test:
// result sets (two identical queries)
var rs1, rs2;
//items to watch (two instances of same model data)
var item1, item2;
dojo.when(store.query({}), function (results) {
rs1 = results;
item1 = results[0];
// item1.get('prop') == 'foo'
function init () {
// find widgets by id:
var myStandby = dijit.byId('basicStandby1'),
myForm = dijit.byId('myForm');
// i don't think you need this line, but i haven't studied the BasicStandby widget
//document.body.appendChild(myStandby.domNode);
dojo.connect(myForm, 'onSubmit', function() {
myStandby.show();
});
attributeMap: {
seconds: {
data: "seconds",
type: "no-dom"
},
displaySeconds: {
source: "seconds",
deriver: "_displaySeconds",
type: "no-dom"
}
cujo.js equivalent (in-browser):
file: myApp.view.LoginPanel.html
<div class="myapp-view-loginpanel">
<span data-attach="greeting">${display.greeting}</span>
<a data-attach="action" href="{$config.loginUrl}">{$display.loginAction}</a>
</div>
logout version extends login version (inheritance defined in myApp.view.LogoutPanel.js:
@unscriptable
unscriptable / bwb18i
Created April 30, 2010 20:46
My comment on nettuts+ that didn't get approved (I wonder why) http://bit.ly/bwb18i
My comment on nettuts+ that didn't get approved (I wonder why) http://bit.ly/bwb18i
It seems that this post started out with good intentions: How to show junior javascript programmers how to use the Firebug profiling commands. However, due to crappy jQuery advice, turned into an illustration of web programming anti-patterns (aka “Programming No-nos”).
I have to agree with James (of course), and would add the following:
———-
Premature optimization.
Do not try to second-guess the framework (jQuery, prototype, dojo, etc.) unless you’ve already got a performance problem. The people who build those frameworks have done an excellent job and have designed them to work well in most situations.
$.each(itemList, function (which) {
// if dev passed an array
if ($.isArray(itemList))
// use the 2nd arg as item name (1st arg is array index)
which = arguments[1];
// check if we've already initialized this item
var already = doneMap[which];
if (!already)
// original (http://html5shiv.googlecode.com/svn/trunk/html5.js)
(function(){if(!/*@cc_on!@*/0)return;var e = "abbr,article,aside,audio,canvas,datalist,details,figure,figcaption,footer,header,hgroup,mark,menu,meter,nav,output,progress,section,summary,time,video".split(','),i=e.length;while(i--){document.createElement(e[i])}})()
// kangax version (29 characters less, yay!)
/*@cc_on(function(e,i){i=e.length;while(i--)document.createElement(e[i])})("abbr,article,aside,audio,canvas,datalist,details,eventsource,figure,footer,header,hgroup,mark,menu,meter,nav,output,progress,section,time,video".split(','))@*/